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

时间:2012-11-29 20:01:35

标签: c++ c++11 directx-11

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

class Example
{
public:
    Destroy();
};

然后当我使用共享ptr时,我就这样做:

std::shared_ptr<Example> ptr(new Example, std::mem_fun(&Example::Destroy));

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

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)'

然后当我明确指定模板参数时:

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

有人可以解释为什么我不能将此功能用作删除器吗?

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

2 个答案:

答案 0 :(得分:14)

这个怎么样?

std::shared_ptr<ID3D11Device> ptr(nullptr, [](ID3D11Device* ptr){ptr->Release();} ); 

答案 1 :(得分:0)

试试这个

struct Releaser{
    void operator()(ID3D11Device* p){
        p->Release();

    };

};


std::shared_ptr<ID3D11Device> ptr(nullptr, Releaser());