所有的標題。它會更容易讀/寫我的例子中的第二行,因爲模板參數的類型是顯而易見的:爲什麼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