c++14之std::make_unique

基础介绍

虽然在c++11版本std::unique_ptr<T>已经引入,但是在c++14版本引入之前,std::unique_ptr<T>的创建还是通过new操作符来完成的。在c++14版本已经引入了类似make_shared的std::make_unique,目的是提供更加安全的方法创建std::unique_ptr<T>,防止产生资源泄露等问题。

为什么要引入std::make_unique<T>?

首先请看下面这句话:

std::make_unique is the recommended way to create instances of std::unique_ptrs due to the following reasons:

  • Avoid having to use the new operator.

       避免使用new操作符

  • Prevents code repetition when specifying the underlying type the pointer shall hold.

        避免类型重复,结合auto关键字可以使用类型推导

  • Most importantly, it provides exception-safety. 

        最重要的是,它提供了异常安全

 下面我们着重分析一下第2点和第3点,先说第二点,避免代码重复(code repetition),应该按下面的方式理解:

//不使用std::make_unique
// 需要重复写类型名
std::unique_ptr<std::vector<std::string>> ptr(new std::vector<std::string>());

// 模板类中更麻烦
template<typename T>
class Container {
    std::unique_ptr<std::vector<T>> data(new std::vector<T>());
};

//使用std::make_unique
// 类型推导,无需重复
auto ptr = std::make_unique<std::vector<std::string>>();

// 模板类中更简洁
template<typename T>
class Container {
    auto data = std::make_unique<std::vector<T>>();
};

现在分析最重要的第3点,即为什么std::make_unique可以提供异常安全。看下面的例子:

foo(std::unique_ptr<T>{new T{}}, function_that_throws(), std::unique_ptr<T>{new T{}});

上例中foo函数为什么不安全呢?原因是编译器并不会规定参数求值的执行顺序,那么可能是顺序是:

  1. 执行new T(参数一)
  2. 执行new T (参数二)
  3. 执行function_that_throws() --抛出异常
  4. 构造std::unique_ptr<T> 参数一
  5. 构造std::unique_ptr<T> 参数二

我们知道std::unique_ptr<T>自身可以管理资源,当std::unique_ptr<T>对象出了其作用后会自动释放资源,但是如果在第3步的时候,抛出了异常,导致后面的std::unique_ptr<T>对象未创建成功,则会导致内存泄露。

而使用std::make_unique<T>方式创建对象,如下所示:

foo(std::make_unique<T>(), function_that_throws(), std::make_unique<T>());

std::make_unique可以保证参数一创建完成,即生成std::unique_ptr<T>后再执行后面的操作,此时由于std::unique_str<T>对象已经生成,即使后续抛出异常,那么这块内存也会得到释放。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值