c++ - 将 std::make_unique 与自定义删除器一起使用

标签 c++ c++11 c++14

在使用 std::unique_ptr我希望使用自定义删除器 std::make_unique而不是一个原始的新。我正在使用 VC++2013。在我看来,没有办法使用std::unique_ptr如果您使用的是自定义删除器。我错过了什么还是真的是这样?


附加信息:

我正在使用 std::unique_ptr<HANDLE, custom_deleter>为打开的 COM 端口保存一个 Windows HANDLE。

我可以为此编写一个自定义的 RAII 类,这不会非常困难,但我看到使用 std::unique_ptr 会有多么困难/困难/糟糕。 .

最佳答案

make_unique 的全部意义在于封装“使用 new 从给定的构造函数参数创建 T 的概念 并使用 delete 将其销毁”。

如果你想要一个自定义删除器,你还必须指定如何创建对象,那么拥有 emplaceing maker 功能就没有什么好处了。

我为某些独特的资源句柄编写了一些自定义 maker 函数的示例 in this post .

关于c++ - 将 std::make_unique 与自定义删除器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21788066/

相关文章:

c++ - std::is_integer 和 std::is_integral 之间的区别?

c++ - std::make_unique<std::thread> 与 lambda

c++ - 如何编写带有 'asm volatile'的btr指令

c++ - 尝试使用 GetModuleFileName 查找可执行文件但返回数字

c++ - 为什么就地成员初始化在 C++11 中使用复制构造函数?

c++ - 尝试实现 std::tie 和 std::tuple 的小版本

C++14类函数错误: "Object has no attribute ' value'"

c++ - 从 QTextBrowser 打开文件

c++ - 没有循环的算法复杂度?

Snow Leopard Xcode 中的 C++ 调试构建中断