【发布时间】:2021-02-23 05:58:46
【问题描述】:
所以我只是在试验智能指针以及如何使用它们管理 Win32 HANDLE 对象,所以我想测试我编写的这个简单代码。
它应该为std::unique_ptr 提供一个自定义删除器,以便在智能指针超出范围时使用。
template <typename Function>
class PtrDeleter
{
Function _Closer;
public:
void operator()(void* memBlock) const
{
_Closer(memBlock);
}
};
//The std::make_unique is the one causing the problem...
std::unique_ptr<void*, PtrDeleter<decltype(CloseHandle)>> ptr = std::make_unique<void*>(OpenThread(...));
我为Function 参数使用模板的原因是因为我将在CloseHandle 中使用其他函数,例如CoTaskMemFree。
无法从 std::unique_ptr
>' 转换为 'std::unique_ptr >
这是编译器输出的。为什么它还在尝试将std::default_delete 与std::make_unique 一起使用?
【问题讨论】:
-
这能回答你的问题吗? Using std::make_unique with a custom deleter
-
WIL 已经实现了这一点。看看RAII resource wrappers。
标签: c++ winapi stl smart-pointers