在派生类上使用带有自定义删除器的 std::make_unique
Using std::make_unique with custom deleter on a derived class?
假设我有一个名为 Derived
的类,它继承自一个名为 Base
的类。 Base
是特殊的 - 虽然可以使用new创建它,但它必须通过自定义删除程序进行破坏。
我想将Base
与名为 BaseDeleter
的自定义删除器一起放在unique_ptr
中。这也允许我从分配给它的Base
派生的其他类。为了异常安全和一致性,我想使用std::make_unique
来维护我的unique_ptr
。
创建了一个小片段来演示我想要什么:
#include <memory>
class Base
{
};
class Derived : public Base
{
};
struct BaseDeleter
{
void operator()(Base* base)
{
// Perform some special deleting
}
};
class Big
{
public:
Big()
{
//pointer.reset(new Derived()); // This works!
pointer = std::make_unique<Derived, BaseDeleter>(); // But this doesn't...
}
private:
std::unique_ptr<Base, BaseDeleter> pointer;
};
int main()
{
Big clazz;
}
不幸的是,这无法在Visual Studio 2015 Update 2和gcc-5.1上编译。(同上)
为什么这不起作用?怎么能用std::make_unique
来分配这样的std::unique_ptr
呢?
这是
make_unique
的签名之一,我想你希望被使用:
template< class T, class... Args >
unique_ptr<T> make_unique( Args&&... args );
其中T
是要创建的对象的类型,Args...
是要转发给构造函数的参数的类型。
如您所见,您无法使用智能指针的make_*
帮助程序函数指示自定义删除器(既不使用 make_unique
,也不使用 make_shared
)。
您必须显式构造指针,如下所示:
std::unique_ptr<T, D> ptr{new T{)};
如果删除器不是默认可构造的,则可以执行以下操作:
std::unique_ptr<T, D> ptr{new T{}, d};
其中d
是删除程序的实例。
Make unique不适用于自定义删除器。 编写自己的或不使用它。
这与你问题中的基础/派生的复杂性无关,这是一个红鲱鱼。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- 如何摆脱导入的 make 项目中的 Eclipse 索引器"Type std::... could not be resolved"错误
- Make zmqpp::socket::connect a std::future
- 为什么是 std::make_unique 而不是 std::unique_ptr::make?
- Travis ci make -std=c++14无法识别
- const decltype(*std::begin(container))& val dont make val const?
- make and std::logic_error