CppCoreGuidelines C.150 使用 make_unique() 构建 unique_ptr
07 January 2023
“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
构造,警告。