07 January 2023

C++ 核心指南目录

“Use make_unique() to construct objects owned by unique_ptr’s”

理由

make_unique 进行对象构造,语句简介,在使用复杂表达式的时候能确保异常安全。

例子

unique_ptr<Foo> p {new Foo{7}};    // OK: but repetitive

auto q = make_unique<Foo>(7);      // Better: no repetition of Foo

强化

  • (简单)如果 unique_ptr 是通过 new ,而不是通过 make_unique 构造,警告。