在direct3d11对象上使用std :: shared_ptr的自定义删除器

Fat*_*sis 5 c++ directx-11 c++11

当我使用std :: shared_ptr并需要一个自定义删除器时,我通常会创建一个对象的成员函数来促进它的破坏,如下所示:

class Example
{
public:
    Destroy();
};
Run Code Online (Sandbox Code Playgroud)

然后当我使用共享ptr时,我只是这样:

std::shared_ptr<Example> ptr(new Example, std::mem_fun(&Example::Destroy));
Run Code Online (Sandbox Code Playgroud)

问题是,现在我正在使用d3d11,我想将com发布函数用作std :: shared_ptr自定义删除器,就像这样

std::shared_ptr<ID3D11Device> ptr(nullptr, std::mem_fun(&ID3D11Device::Release));
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

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)'
Run Code Online (Sandbox Code Playgroud)

然后当我明确指定模板参数时,如下所示:

std::shared_ptr<ID3D11Device> ptr(nullptr, std::mem_fun<ULONG, ID3D11Device>(&ID3D11Device::Release));
Run Code Online (Sandbox Code Playgroud)

我收到这个错误,

error C2665: 'std::mem_fun' : none of the 2 overloads could convert all the argument types
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么我不能将此功能用作删除器?

注意:不建议我使用CComPtr,我使用的是msvc ++ express版:

Arn*_*rtz 14

这个怎么样?

std::shared_ptr<ID3D11Device> ptr(nullptr, [](ID3D11Device* ptr){ptr->Release();} ); 
Run Code Online (Sandbox Code Playgroud)

  • @FatalCatharsis:Lambdas具有匿名(不可命名)类型,每个类型都是唯一的并由编译器生成. (5认同)