gpt4 book ai didi

c++ - 将 mem_fun_ref 与 boost::shared_ptr 一起使用

转载 作者:太空狗 更新时间:2023-10-29 23:38:01 25 4
gpt4 key购买 nike

遵循 this page 的建议,我试图让 shared_ptr 调用 IUnknown::Release()而不是删除:

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

我不知道该怎么做。我有限的模板/仿函数知识促使我尝试

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

但是没有用。有什么想法吗?

最佳答案

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

您可以改用 boost::mem_fn。你should define BOOST_MEM_FN_ENABLE_STDCALL 使用 COM 方法。

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

并且由于您的对象具有内部引用计数,您可以考虑使用 boost::intrusive_ptr相反。

关于c++ - 将 mem_fun_ref 与 boost::shared_ptr 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2831438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com