所有的标题。它会更容易读/写我的例子中的第二行,因为模板参数的类型是显而易见的:为什么std :: unique_ptr不允许类型推断?
#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),所以不要重复。 – Jarod42