如此处所示,std::unique_ptr
有两个针对空指针的constexpr
构造函数:
constexpr unique_ptr();
constexpr unique_ptr( nullptr_t );
我对这两个构造函数有两个问题:
为什么我们需要两个构造函数?我们不能只声明一个,如下:
constexpr unique_ptr( nullptr_t = nullptr );
constexpr
真的有用吗?我在我的代码中尝试使用它,但它无法编译(g++ 6.1.0,-std=c++14
):constexpr std::unique_ptr<int> p; // error: the type 'const std::unique_ptr<int>' of constexpr variable 'p' // is not literal because 'std::unique_ptr<int>' has a non-trivial destructor