Using mem_fun_ref with boost :: shared_ptr
Following the advice on this page , I'm trying to get a shared_ptr to call IUnknown :: Release () instead of deleting:
IDirectDrawSurface* dds;
... //Allocate dds
return shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(&IUnknown::Release));
error C2784: 'std :: const_mem_fun1_ref_t <_Result, _Ty, _Arg> std :: mem_fun_ref (_Result (__thiscall _Ty :: *) (_ Arg) const)': Failed to deduce template argument for '_Result (__thiscall _Ty ) (_ Arg) const 'from' ULONG (__cdecl IUnknown :: *) (void) '
error C2784: 'std :: const_mem_fun_ref_t <_Result, _Ty> std :: mem_fun_ref (_Result (__thiscall _Ty :: *) (void) const)': failed to deduce template argument for '_Result (__thiscall _Ty :: *) (void ) const 'from' ULONG (__cdecl IUnknown :: *) (void) '
error C2784: 'std :: mem_fun1_ref_t <_Result, _Ty, _Arg> std :: mem_fun_ref (_Result (__thiscall _Ty :: *) (_ Arg))': failed to deduce template argument for '_Result (__thiscall _Ty :: *) (_ Arg) 'from' ULONG (__cdecl IUnknown :: *) (void) '
error C2784: 'std :: mem_fun_ref_t <_Result, _Ty> std :: mem_fun_ref (_Result (__thiscall _Ty :: *) (void))': failed to deduce template argument for '_Result (__thiscall _Ty :: *) (void) 'from' ULONG (__cdecl IUnknown :: *) (void) '
error C2661: 'boost :: shared_ptr :: shared_ptr': no overloaded function takes 2 arguments
I have no idea what to do about it. My limited knowledge of templates / functors made me try
typedef ULONG (IUnknown::*releaseSignature)(void);
shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(static_cast<releaseSignature>(&IUnknown::Release)));
But to no avail. Any ideas?
a source to share
std::mem_fun_ref
does not support conversion stdcall
as well std::mem_fun
, which you can use for pointers.
You can use instead boost::mem_fn
. You must define BOOST_MEM_FN_ENABLE_STDCALL
to work with COM methods.
shared_ptr<IDirectDrawSurface>( dds, boost::mem_fn(&IUnknown::Release) );
And since your object has an internal reference count, you can use instead boost::intrusive_ptr
.
a source to share
I know it might not be what you are after, but just include ATLBase.h and then use the CComPtr template.
Then you just use
CComPtr< IDirect3DSurface9 > surf;
pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &surf );
Then you can copy it to another CComPtr and handle all AddRefs and Releases for you. A very useful template class.
a source to share