带有自定义删除器const转换的C ++ std :: unique_ptr

时间:2017-02-21 15:00:33

标签: c++11 const unique-ptr

以下代码在C ++ 11中有效,可用于转换为基础指针类型的const版本。

std::unique_ptr<int> a = std::unique_ptr<int>(new int(10));
std::unique_ptr<const int> b = std::move(a);

将unique_ptr与自定义删除器一起使用时是否可以实现相同的功能?

std::unique_ptr<int, std::function<void(int*)>> a = std::unique_ptr<int, std::function<void(int*)>>(new int(10), [](int*){});
std::unique_ptr<const int, ???> b = std::move(a);

正如我所料(由于不同的类型),我得到编译器错误,无论我是使用int *还是const int *作为const定义中的deleter参数。

某些上下文:我尝试输入一个与自定义分配器一起使用的插件替换unique_ptr签名,并且可以在任何使用unique_ptr的地方使用,例如。

template <typename T> using CustomUniquePtr = std::unique_ptr<T, std::function<void(???)>>;

0 个答案:

没有答案
相关问题