Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mem_fun_ref with boost::shared_ptr

Following the advice of this page, I'm trying to get shared_ptr to call IUnknown::Release() instead of delete:

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)' : could not 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)' : could not 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))' : could not 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))' : could not 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 make of this. My limited template/functor knowledge led me to try

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

But to no avail. Any ideas?

like image 725
BlueRaja - Danny Pflughoeft Avatar asked Jan 19 '26 00:01

BlueRaja - Danny Pflughoeft


2 Answers

std::mem_fun_ref doesn't support stdcall calling conversion as well as std::mem_fun which you could use for pointers.

You could use boost::mem_fn instead. You should 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 the internal reference count you could consider using boost::intrusive_ptr instead.

like image 159
Kirill V. Lyadvinsky Avatar answered Jan 21 '26 15:01

Kirill V. Lyadvinsky


I know this maynot be what youa re after but just include ATLBase.h and then use the CComPtr template.

You then just use

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

You can then copy it to another CComPtr and it handles all the AddRefs and Releases for you. Very useful template class.

like image 32
Goz Avatar answered Jan 21 '26 15:01

Goz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!