问题描述
当我使用std :: shared_ptr并需要一个自定义的删除,我通常做一个成员函数的对象,以方便它的销毁像这样:
When i use an std::shared_ptr and need a custom deleter, i usually make a member function of the object to facilitate it's destruction like this:
class Example
{
public:
Destroy();
};
然后当我使用共享ptr时,我只是这样:
and then when i use the shared ptr, i just make it like this:
std::shared_ptr<Example> ptr(new Example, std::mem_fun(&Example::Destroy));
问题是,现在我使用d3d11,我想使用com发布函数作为std :: shared_ptr自定义删除器,像这样
Problem is, right now i'm working with d3d11, and i would like to use the com release functions as an std::shared_ptr custom deleter, like this
std::shared_ptr<ID3D11Device> ptr(nullptr, std::mem_fun(&ID3D11Device::Release));
但我得到这个错误:
error C2784: 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Result (__thiscall _Ty::* )(_Arg) const' from 'ULONG (__stdcall IUnknown::* )(void)'
,然后当我明确指定模板参数时:
and then when i explicitly specify the template paramters like this:
std::shared_ptr<ID3D11Device> ptr(nullptr, std::mem_fun<ULONG, ID3D11Device>(&ID3D11Device::Release));
我收到此错误,
error C2665: 'std::mem_fun' : none of the 2 overloads could convert all the argument types
有人可以解释为什么我不能使用这个函数作为删除者?
can someone explain why i can't use this function as a deleter?
注意:不建议我使用CComPtr, m使用msvc ++ express版本:\
note: don't suggest I use CComPtr, i'm using msvc++ express edition :\
推荐答案
如何?
std::shared_ptr<ID3D11Device> ptr(nullptr, [](ID3D11Device* ptr){ptr->Release();} );
这篇关于对direct3d11对象使用std :: shared_ptr的自定义删除器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!