Using boost::shared_ptr with WSACleanup
-
08-02-2021 - |
Domanda
I would like to use a boost::shared_ptr in order for WSACleanup() to be called when my function goes out of scope, like this:
void DoSomething() {
WSAStartup(...);
boost::shared_ptr<void> WSACleaner(static_cast<void*>(0), WSACleanup);
}
This does not compile,
Error 1 error C2197: 'int (__stdcall *)(void)' : too many arguments for call C:\projects\svn-5.3\ESA\Common\include\boost\detail\shared_count.hpp 116
any thoughts?
Soluzione
From the docs: "The expression d(p) must be well-formed" (i.e. WSACleanup(static_cast<void*>(0)
must be well-formed.)
One possible solution:
boost::shared_ptr<void> WSACleaner(static_cast<void*>(0),
[](void* dummy){WSACleanup();});
Altri suggerimenti
You can create a class A
which destructor invoques WSACleanup
and instance of shared_ptr with it:
class A
{
public:
~A() { WSACleanup(...); }
}
....
void DoSomething() {
WSAStartup(...);
boost::shared_ptr<A> x(new A);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow