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

标签 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++ 速成版:\

最佳答案

这个怎么样?

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

关于c++ - 在 direct3d11 对象上使用 std::shared_ptr 的自定义删除器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13633737/

相关文章:

c++ - OpenGL/GLUT 光照没有意义

c# - Typedefs、C++/CLI、C# + 依赖注入(inject)

c++ - 设置滚动条有问题

c++ - 为什么 std::boolalpha 在使用 clang 时忽略字段宽度?

c++ - 使用 OpenCV 修改图像时 RGB 值错误

C++ 如何设置 `std::exponential_distribution` 对象的参数?

c++ - 指向数组子集的智能指针 (c++11)

c++ - DrawInstanced 与 DrawIndexed 并将它们混合在一起

c++ - 如何将世界坐标转换为屏幕坐标

DirectX11 : Pass data from ComputeShader to VertexShader?