2010-05-14 37 views
2

this page的建议,我试图让shared_ptr的调用IUnknown::Release()而不是删除:使用mem_fun_ref与升压:: shared_ptr的

IDirectDrawSurface* dds; 
... //Allocate dds 
return shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(&IUnknown::Release)); 

错误C2784:“的std :: const_mem_fun1_ref_t < _result, _Ty,_Arg>的std :: mem_fun_ref(_result(__thiscall _Ty :: *)(_ ARG)常量) ':不能推导出模板参数'从' ULONG _result(__thiscall _Ty :: *)(_ ARG)常量(__cdecl的IUnknown :: *)(无效) '

错误C2784:' 的std :: const_mem_fun_ref_t < _result, _Ty> std :: mem_fun_ref(_Result(__thiscall _Ty :: *)(void)const)':无法为'_Result(__thiscall _Ty :: *)(void)const'从'ULONG(__cdecl IUnknown :: *)(无效)”

错误C2784: '的std :: mem_fun1_ref_t < _result,_Ty,_Arg>的std :: mem_fun_ref(_result(__thiscall _Ty :: *)(_ ARG))':不能推导出模板参数为'_result(__thiscall _Ty :: *)(_精氨酸)' 从 'ULONG(__cdecl的IUnknown :: *)(无效)'

错误C2784:“的std :: mem_fun_ref_t < _result,_Ty>的std :: mem_fun_ref(_result (__thiscall _Ty :: *)(无效))”:不能推导出模板参数'从 'ULONG _result(__thiscall _Ty :: *)(无效)(__cdecl的IUnknown :: *)(无效)'

错误C2661: '提高:: shared_ptr的:: shared_ptr的':没有重载函数采用2个参数

我不知道本作的是什么。我有限的模板/函数知识让我去尝试

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

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

回答

2

调用约定说明符不是问题吗?这会好吗?

void iUnk_delete(IUnknown* u) { 
    u->Release(); 
} 


return shared_ptr<IDirectDrawSurface>(dds, iUnk_delete); 
+0

呃,哇,工作。谢谢!你知道为什么[这个问题]的答案(http://stackoverflow.com/questions/441306/make-shared-ptr-not-use-delete)完全使用'ptr_fun'吗? – 2010-05-14 02:22:45

6

std::mem_fun_ref不支持stdcall调用转换以及std::mem_fun,你可以使用指针。

你可以使用boost::mem_fn代替。您可以使用COM方法should defineBOOST_MEM_FN_ENABLE_STDCALL

shared_ptr<IDirectDrawSurface>(dds, boost::mem_fn(&IUnknown::Release)); 

而且,由于你的对象有内部引用计数,你可以考虑使用boost::intrusive_ptr代替。

3

我知道这maynot是您最热衷后重新只是包括ATLBase.h然后使用但是CComPtr模板。

你就用

CComPtr<IDirect3DSurface9> surf; 
pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &surf); 

然后,您可以将其复制到另一个但是CComPtr,它处理所有的AddRefs和发布的为您服务。非常有用的模板类。