将 std::make_unique 与自定义删除器一起使用
Using std::make_unique with a custom deleter
在将std::unique_ptr
与自定义删除器一起使用时,我希望使用std::make_unique
而不是原始的新。 我正在使用VC++2013。 在我看来,如果您使用的是自定义删除器,则无法使用std::unique_ptr
。 是我错过了什么,还是真的是这样?
附加信息:
我正在使用std::unique_ptr<HANDLE, custom_deleter>
来持有打开的COM端口的Windows句柄。
我可以为此编写一个自定义 RAII 类,这不会非常困难,但我看到使用 std::unique_ptr
会有多难/困难/糟糕。
make_unique
的全部意义在于封装"使用 new
从给定的构造函数参数创建T
并使用delete
来销毁它"的概念。
如果你想要一个自定义删除器,你还必须指定如何创建对象,然后使用放置 maker 函数就没有什么好处了。
我在这篇文章中为某些独特的资源句柄编写了一些自定义制作者函数的示例。
这是一种使用调用自定义自由函数的自定义删除器将 c 样式内存管理包装到std::unique_ptr
中的方法。这有一个类似于 std::make_unique
LIVE 的 make 函数助手:
#include <iostream>
#include <functional>
#include <memory>
// Some C style code that has some custom free function ptr...
extern "C" {
struct ABC { };
enum free_type_e {
FREE_ALL,
FREE_SOME
};
typedef void (free_f)(enum free_type_e free_type, void *ptr);
struct some_c_ops { free_f* free_op; };
void MY_free(enum free_type_e free_type, void *ptr)
{
printf("%s:%d ptr=%ldn", __func__, __LINE__, (long)ptr);
(void)free_type;
free(ptr);
}
}; // extern "C"
template<typename T>
using c_unique_ptr = std::unique_ptr<T,std::function<void(T*)>>;
template <typename T>
c_unique_ptr<T> make_c_unique(some_c_ops* op, free_type_e free_type)
{
return c_unique_ptr<T>(static_cast<T*>(calloc(1, sizeof(T))),
std::bind(op->free_op, free_type, std::placeholders::_1));
}
void foo(c_unique_ptr<ABC> ptr)
{
std::cout << __func__ << ":" << __LINE__
<< " ptr=" << reinterpret_cast<size_t>(ptr.get()) << std::endl;
}
int main()
{
some_c_ops ops = { MY_free };
c_unique_ptr<ABC> ptr = make_c_unique<ABC>(&ops, FREE_ALL);
std::cout << __func__ << ":" << __LINE__
<< " ptr=" << reinterpret_cast<size_t>(ptr.get()) << std::endl;
foo(std::move(ptr));
std::cout << __func__ << ":" << __LINE__
<< " ptr=" << reinterpret_cast<size_t>(ptr.get()) << std::endl;
}
可能的输出:
main:48 ptr=50511440 foo:40 ptr=50511440 MY_free:20 ptr=50511440 main:53 ptr=0
据
我所知,C++11标准中没有make_unique
功能。看
- 为什么C++11有"make_shared"而不是"make_unique"
- make_unique和完美的转发
因此,我假设make_unique
是至少未包含在标准中的Microsoft实现。
但是,尽管如此,您仍然可以将自定义删除程序与 unique_ptr
.使用 unique_ptr
时,必须将删除器的类型指定为第二个模板参数,然后将适当的对象传递给构造函数。
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何将enable-if与模板参数和参数包一起使用
- C++将已删除的函数 std::unique_ptr 与基类一起使用
- shared_ptr自定义分配器与自定义删除器一起
- 有没有办法将擦除-删除成语与其他循环条件一起使用
- 删除功能误差的C 与有条件的iniaization一起使用
- 将 std::make_unique 与自定义删除器一起使用
- QT删除可与Valgrind一起使用,但不能正常执行
- 为什么这种将emplace_back与已删除的复制构造函数一起使用不起作用?
- 将 std::unique_ptr<void> 与自定义删除器一起使用作为智能无效*
- 将基于范围的 for 与 std::set<std::unique_ptr <T>一起使用>已删除函数