【发布时间】:2016-12-15 11:19:00
【问题描述】:
由于模板参数的类型很明显,因此读/写示例的第二行会更容易:
#include <memory>
struct Foo{};
int main()
{
// redundant: good
auto foo1 = std::unique_ptr<Foo>(new Foo());
// without explicitness: does not compile
auto foo2 = std::unique_ptr(new Foo());
}
当然,如果你想使用多态,我们总是可以这样写:
auto base = std::unique_ptr<Base>(new Derived());
这种约束的原因是什么?
【问题讨论】:
-
使用
std::make_unique(c++14),所以不再重复。
标签: c++ c++11 unique-ptr