std::make_unique 是 c++14 引入的安全构造 std::unique_ptr 的辅助函数,解决 new 与 unique_ptr 手动组合时因参数求值异常导致的内存泄漏问题;它通过一步完成分配与封装,保证异常安全,并支持完美转发参数构造对象或数组,但不支持初始化列表、抽象类或私有构造函数类型。

std::make_unique 是什么,为什么不能直接 new
std::make_unique 是 C++14 引入的辅助函数,用于安全构造 std::unique_ptr。它不是语法糖,而是解决 new + unique_ptr 手动组合时潜在的异常安全问题:如果构造参数求值过程中抛出异常(比如某个参数是函数调用且失败),new 已执行但 unique_ptr 未完成接管,就会导致内存泄漏。
正确做法是让分配和封装一步完成,由 make_unique 内部保证——要么全成功,要么不分配。
基本用法与参数转发规则
std::make_unique 支持三种形式:make_unique<t>()</t>、make_unique<t>(args...)</t>、make_unique<t>(n)</t>。它通过完美转发把参数传给 T 的构造函数或数组长度。
- 对于非数组类型,
args...必须匹配T的构造函数签名;不支持初始化列表语法(如{1,2,3}),会编译失败 - 对于数组类型,只接受一个
size_t参数,不支持带初始值的数组(C++17 才支持make_unique<t>(n, args...)</t>) - 不能用于抽象类或无公开构造函数的类型(和直接
new一样受访问控制限制)
auto p1 = std::make_unique<std::string>("hello"); // OK
auto p2 = std::make_unique<int>(42); // OK
auto p3 = std::make_unique<std::vector<int>>(5, 0); // OK: 转发到 vector(size, value)
auto p4 = std::make_unique<int[]>(10); // OK: 分配 10 个 int 的数组
// auto p5 = std::make_unique<std::string>({"a", "b"}); // 错误!不支持 braced-init-list
常见错误:和 std::make_shared 混用,或误传裸指针
std::make_unique 返回的是 std::unique_ptr,不是裸指针,也不接受裸指针作为参数。有人误以为它类似 make_shared 可以“包装已有对象”,这是错的。
立即学习“C++免费学习笔记(深入)”;
- 不能写
std::make_unique<t>(new T{...})</t>—— 这会造成双重分配,且立即泄漏 - 不能对已存在的对象使用
make_unique,它只负责“分配+构造”一体化 - 不要用
std::make_unique替代std::shared_ptr场景:若需共享所有权,应选std::make_shared;二者语义和性能开销不同(make_shared合并控制块与对象内存,make_unique不做此优化)
异常安全的实际验证点
真正体现 make_unique 价值的地方,是构造函数可能抛异常、且参数本身有副作用时。例如:
struct MayThrow {
MayThrow(int x) { if (x < 0) throw std::runtime_error("bad"); }
};
// 危险写法(不推荐):
// auto ptr = std::unique_ptr<MayThrow>(new MayThrow(get_value()));
// 若 get_value() 正常,但 MayThrow 构造抛异常,则 new 分配的内存泄漏
// 安全写法:
auto ptr = std::make_unique<MayThrow>(get_value()); // 全或无:要么 ptr 持有对象,要么没分配
注意:即使 get_value() 抛异常,make_unique 也不会触发分配;只有所有参数求值成功后,才调用 new 和构造函数。
真正容易被忽略的是——这个保障只在「单次 make_unique 调用内」有效。如果你写 func(make_unique<a>(), make_unique<b>())</b></a>,而 B 的构造失败,A 已分配却无法回滚(C++ 中函数参数求值顺序未定义,且无事务机制)。这种场景需拆成独立语句或用 guard 模式。











