سؤال

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?

هل كانت مفيدة؟

المحلول

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();});

نصائح أخرى

You can create a class Awhich 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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top