【发布时间】:2015-05-29 00:58:54
【问题描述】:
我一直在阅读GOTW102,想知道为什么make_unique 比其他情况更安全,或者详细说明为什么f(new T(...)) 比f(new T1(...), new T2(...)) 更安全。
博客中的make_unique 实现如下:
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}
现在我想知道f(new T(...)) 是否通常是异常安全的(无泄漏),或者在make_unique 的情况下是否只是异常安全,因为std::unique_ptr 的构造函数不会抛出的附加知识? (因为如果按照我的理解,新构建的T 无论如何都会被泄露。
【问题讨论】:
标签: c++ exception c++14 exception-safety