将mem_fun_ref与boost :: shared_ptr一起使用

Blu*_*eft 2 c++ directx boost shared-ptr

按照这个页面的建议,我试图让shared_ptr调用IUnknown :: Release()而不是delete:

IDirectDrawSurface* dds;
... //Allocate dds
return shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(&IUnknown::Release));
Run Code Online (Sandbox Code Playgroud)

错误C2784:'std :: const_mem_fun1_ref_t <_Result,_Ty,_Arg> std :: mem_fun_ref(_Result(__ thishisall _Ty ::*)(_ Arg)const)':无法推断'_Result的模板参数(__thiscall _Ty ::*) (_Arg)const'from'ULONG(__ cdecl IUnknown ::*)(void)'

错误C2784:'std :: const_mem_fun_ref_t <_Result,_Ty> std :: mem_fun_ref(_Result(__ thishisall_Ty ::*)(void)const)':无法推断'_Result的模板参数(__thiscall _Ty ::*)(void )'''来自'ULONG(__ cdecl IUnknown ::*)(void)'

错误C2784:'std :: mem_fun1_ref_t <_Result,_Ty,_Arg> std :: mem_fun_ref(_Result(__ thiscall _Ty ::*)(_ Arg))':无法推断出'_Result(__thiscall _Ty ::*)的模板参数( _Arg)'from'ULONG(__ cdecl IUnknown ::*)(void)'

错误C2784:'std :: mem_fun_ref_t <_Result,_Ty> std :: mem_fun_ref(_Result(__ thishisall _Ty ::*)(void))':无法推断'_Result的模板参数(__thiscall _Ty ::*)(void) 'from'ULONG(__ cdecl IUnknown ::*)(void)'

错误C2661:'boost :: shared_ptr :: shared_ptr':没有重载函数需要2个参数

我不知道该怎么做.我有限的模板/函子知识让我尝试

typedef ULONG (IUnknown::*releaseSignature)(void);
shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(static_cast<releaseSignature>(&IUnknown::Release)));
Run Code Online (Sandbox Code Playgroud)

但无济于事.有任何想法吗?

Kir*_*sky 6

std::mem_fun_ref不支持stdcall调用转换以及std::mem_fun可用于指针的转换.

你可以boost::mem_fn改用.您应该定义 BOOST_MEM_FN_ENABLE_STDCALL使用COM方法.

shared_ptr<IDirectDrawSurface>( dds, boost::mem_fn(&IUnknown::Release) );
Run Code Online (Sandbox Code Playgroud)

而且由于您的对象具有内部引用计数,您可以考虑使用它boost::intrusive_ptr.