tsotso tsotso -4 years ago 159
C++ Question

Using boost::shared_ptr with WSACleanup

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() {
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?

Answer Source

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();});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download