1

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?

4

2 回答 2

1

您可以使用它创建一个A析构函数调用WSACleanup和 shared_ptr 实例的类:

class A
{
    public:
        ~A() { WSACleanup(...); }
}

....

void DoSomething() {
    WSAStartup(...);
    boost::shared_ptr<A> x(new A);
}
于 2011-10-11T12:17:55.653 回答
1

来自文档:“表达式 d(p) 必须格式正确”(即WSACleanup(static_cast<void*>(0)必须格式正确。)

一种可能的解决方案:

boost::shared_ptr<void> WSACleaner(static_cast<void*>(0),
                                   [](void* dummy){WSACleanup();});
于 2011-10-11T12:14:40.170 回答