对 Direct3D11 对象上使用 std::shared_ptr 的自定义删除程序
using a custom deleter for std::shared_ptr on a direct3d11 object
当我使用 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();} );
试试这个
struct Releaser{
void operator()(ID3D11Device* p){
p->Release();
};
};
std::shared_ptr<ID3D11Device> ptr(nullptr, Releaser());
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 无法使用 libtool 将 -shared 参数传递给 g++
- boost::shared_ptr和std::shared-ptr的同居
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- shared-ptr-C++shared_ptr与unique_ptr用于资源管理