我想使用boost::shared_ptr,以便在函数超出范围时调用WSACleanup(),如下所示:
void DoSomething() {
WSAStartup(...);
boost::shared_ptr<void> WSACleaner(static_cast<void*>(0), WSACleanup);
}这不能编译,
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
有什么想法吗?
发布于 2011-10-11 20:14:40
文档中写道:“表达式d(p)必须是良构的”(即WSACleanup(static_cast<void*>(0)必须是良构的。)
一种可能的解决方案是:
boost::shared_ptr<void> WSACleaner(static_cast<void*>(0),
[](void* dummy){WSACleanup();});发布于 2011-10-11 20:17:56
您可以创建一个类A,它使用析构函数调用WSACleanup和shared_ptr的实例:
class A
{
public:
~A() { WSACleanup(...); }
}
....
void DoSomething() {
WSAStartup(...);
boost::shared_ptr<A> x(new A);
}https://stackoverflow.com/questions/7725821
复制相似问题