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