std::unique_ptr

std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权。由C++11标准引入,用于替代C++98中过时的std::auto_ptr智能指针。相比而言,std::unique_ptr的优点有:

  • 语义更清晰std::auto_ptr进行拷贝的时候实际执行的是移动语义,但C++98中并没有定义出移动语义,所以使用的时候可能会违背直觉。而std::unique_ptr利用了C++11中新定义的移动语义,只允许移动操作,禁止拷贝操作,从而让语义更加清晰。
  • 允许自定义删除器:由于std::unique_ptr将删除器作为自己的成员变量,所以传入自定义删除器之前需要在模板参数中指定删除器的类型std::unique_ptr<T, D> up(nullptr, deleter)
  • 支持STL容器:在C++98中,容器要求元素必须是可以拷贝的,比如«effective STL»中提到的,对容器中的元素进行std::sort时,会从区间中选一个元素拷贝为主元素(pivot),然后再对所有元素进行分区操作。但是std::auto_ptr的拷贝操作执行的却是移动语义,这样就会造成bug。在C++11中,STL容器是支持移动语义的,std::unique_ptr只提供移动操作删除了拷贝操作,并且移动操作是noexcept的(这一点很重要,因为STL容器有些操作需要保证强异常安全会要求要么用拷贝操作要么用无异常的移动操作)。只要不涉及到拷贝的容器操作,比如fill函数,那么std::unique_ptr作为容器元素是正确的。

std::unique_ptr的大小

默认情况下,std::unique_ptr的大小和原始指针一样:

#include <iostream>
#include <memory> int main(int argc, const char* argv[]) {
std::unique_ptr<int> upNum(new int); // 输出8(64位操作系统)
std::cout << sizeof(upNum) << std::endl; return 0;
}

当添加删除器的时候,情况就发生了变化,std::unique_ptr的大小就等于原始指针的大小加上删除器类型的大小:

#include <iostream>
#include <memory> void deleter(int* pNum) {
std::cout << "function deleter" << std::endl;
delete pNum;
} int main(int argc, const char* argv[]) {
std::unique_ptr<int, decltype(&deleter)> upNum(new int, deleter); // 输出8+8=16(函数指针类型的大小也为8)
std::cout << sizeof(upNum) << std::endl; return 0;
}

这种情况是可以优化的,那就是使用仿函数或者lambda函数作为删除器,当仿函数或lambda函数是无状态(stateless)的时候,那么类类型作为类成员变量的时候,会自动优化掉空类所占用的空间:

#include <iostream>
#include <memory> int main(int argc, const char* argv[]) {
auto deleter = [](int* pNum) {
std::cout << "lambda deleter" << std::endl;
delete pNum;
};
std::unique_ptr<int, decltype(deleter)> upNum(new int, deleter); // 输出8
std::cout << sizeof(upNum) << std::endl; return 0;
}

使用场景1-工厂模式

#include <iostream>
#include <memory> class Foo {
public:
void greeting() noexcept {
std::cout << "hi! i am foo" << std::endl;
}
}; class Factory {
public:
std::unique_ptr<Foo> createFoo() {
return std::unique_ptr<Foo>(new Foo);
}
}; int main(int argc, const char* argv[]) {
auto foo = Factory().createFoo(); // 输出"hi! i am foo"
foo->greeting(); return 0;
}

使用场景2-实现pImpl模式

PIMPL(Pointer to Implementation)是通过将所有成员变量封装到私有的Impl结构体中,自身只保留一个指向结构体对象的一个私有的指针成员。

pImpl模式的优点有:1.降低模块的耦合度2.提高编译速度3.提高接口稳定性

// Foo.h
#pragma once #include <memory>
#include <string> class Foo {
public:
Foo(); // 需要将~Foo的实现放入Foo.cpp中,避免出现delete imcomplete type错误
~Foo(); // 1.定义了~Foo之后不会自动生成移动函数
// 2.移动构造函数中因为会生成处理异常的代码,所以需要析构成员变量,也会造成delete imcomplete type问题,所以将实现放入Foo.cpp
// 3.移动赋值函数中因为会先删除自己指向的Impl对象指针,也会造成delete imcomplete type问题,所以将实现放入Foo.cpp
Foo(Foo&& rhs) noexcept;
Foo& operator=(Foo&& rhs) noexcept; // 由于unique_ptr不支持复制,所以无法生成默认拷贝函数
Foo(const Foo& rhs);
Foo& operator=(const Foo& rhs); void setName(std::string name);
const std::string& getName() const noexcept; private:
struct Impl;
std::unique_ptr<Impl> m_upImpl;
};
// Foo.cpp
#include "Foo.h"
#include <string> struct Foo::Impl {
std::string name;
}; Foo::Foo() : m_upImpl(new Impl) {} Foo::~Foo() = default; Foo::Foo(Foo&& rhs) noexcept = default;
Foo& Foo::operator=(Foo&& rhs) noexcept = default; Foo::Foo(const Foo& rhs) : m_upImpl(new Impl) {
*m_upImpl = *rhs.m_upImpl;
} Foo& Foo::operator=(const Foo& rhs) {
*m_upImpl = *rhs.m_upImpl;
return *this;
} void Foo::setName(std::string name) {
m_upImpl->name = name;
} const std::string& Foo::getName() const noexcept {
return m_upImpl->name;
}

尽量使用std::make_unique

使用std::make_unique来创建std::unique_ptr智能指针有以下优点:

  • 减少代码重复:从代码std::unique_ptr<Foo> upFoo(new Foo);auto upFoo = std::make_unique<Foo>();可以得知使用make_unique只需要写一次Foo就可以,更加符合软件工程中的要求。

  • 提高异常安全性:当在函数调用中构造智能指针时,由于执行顺序的不确定性,有可能会造成资源泄露,比如对于代码:

    #include <iostream>
    #include <memory>
    #include <exception> bool priority() {
    throw std::exception(); return true;
    } void func(std::unique_ptr<int> upNum, bool flag) {
    if (flag) {
    std::cout << *upNum << std::endl;
    }
    } int main() {
    func(std::unique_ptr<int>(new int), priority()); return 0;
    }

    这里调用func函数时,会执行三个步骤

    1. new int
    2. std::unique_ptr<int>构造函数
    3. priority函数

    这里唯一可以确定的就是步骤1发生在步骤2之前,但步骤3的次序是不一定的,如果步骤3在步骤1和步骤2中间执行那么就会造成内存泄漏。但是如果使用make_unique就不会出现这个问题。

但是std::make_unique是C++14标准才引入的,所以使用C++11环境的话需要自己实现这个函数:

template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&&... params)
{
return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}
  • 素材来自于«effective modern c++»

智能指针(1)-std::unique_ptr的更多相关文章

  1. C++11智能指针之std::unique_ptr

    C++11智能指针之std::unique_ptr   uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...

  2. c++——智能指针学习(unique_ptr)

    1.为什么会有unique_ptr? 动态内存忘记delete,导致内存泄漏.比如: p = new (); if(...) { return ; } delete p; 因此我们需要一种方式来解决这 ...

  3. 【校招面试 之 C/C++】第27题 C++ 智能指针(三)之 unique_ptr

    auto_ptr<string> p1(new string ("auto") : //#1 auto_ptr<string> p2; //#2 p2 = ...

  4. C++智能指针: auto_ptr, shared_ptr, unique_ptr, weak_ptr

    本文参考C++智能指针简单剖析 内存泄露 我们知道一个对象(变量)的生命周期结束的时候, 会自动释放掉其占用的内存(例如局部变量在包含它的第一个括号结束的时候自动释放掉内存) int main () ...

  5. C++11 新特性之智能指针(shared_ptr, unique_ptr, weak_ptr)

    这是C++11新特性介绍的第五部分,涉及到智能指针的相关内容(shared_ptr, unique_ptr, weak_ptr). shared_ptr shared_ptr 基本用法 shared_ ...

  6. C++ template的一些高级用法(元编码,可变参数,仿函数,using使用方法,. C++ 智能指针)

    1 .  通用函数可变参数模板 对于有些时候,我们无法确切的知道,函数的参数个数时,而又不想过多的使用所谓的函数重载,那么就可以效仿下面的例子: #include<iostream> #i ...

  7. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  8. C++11 unique_ptr智能指针详解

    在<C++11 shared_ptr智能指针>的基础上,本节继续讲解 C++11 标准提供的另一种智能指针,即 unique_ptr 智能指针. 作为智能指针的一种,unique_ptr ...

  9. c++智能指针的使用,shared_ptr,unique_ptr,weak_ptr

    c++智能指针的使用 官方参考 普通指针的烦恼:内存泄漏,多次释放,提前释放 智能指针 负责自动释放所指向的对象. 三种智能指针 shared_ptr,unique_ptr,weak_ptr: 将sh ...

随机推荐

  1. leetcode刷题-67二进制求和

    题目 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" ...

  2. PyCharm怎样添加Qt designer

    cmd命令检查是否安装一下工具包 不存在,用pip 命令安装 添加环境变量; QT_QPA_PLATFORM_PLUGIN_PATH    ---D:\VNConda\Lib\site-package ...

  3. ParticleSystem的小优化

    最近游戏场景希望加入一些ParticleSystem来丰富场景,这样肯定会带来一定的性能开销.一般来说ParticleSystem的优化就是尽量减少粒子数,尽量不使用粒子碰撞等.而今天要说的是,多个P ...

  4. mysqli报错注入常见函数

    以下均摘自<代码审计:企业级Web代码安全架构>一书 1.floor() select * from test where id=1 and (select 1 from (select ...

  5. Linux高级命令进阶

    输出重定向 场景:一般命令的输出都会显示在终端中,有些时候需要将一些命令的执行结果想要保存到文件中进行后续的分析/统计,则这时候需要使用到的输出重定向技术. >:覆盖输出,会覆盖掉原先的文件内容 ...

  6. Vue Render自定义tabled单元格内容

    解决问题 只举一个例子(我正好需要用到的) 在写中后台时, 如果对 表格组件 再度封装了, 比如这样的 以element-ui 为例: <template> <el-table :d ...

  7. chrome插件: yapi 接口TypeScript代码生成器

    前言 2020-09-12 天气晴,蓝天白云,微风,甚好. 前端Jser一枚,在公司的电脑前,浏览器打开着yapi的接口文档,那密密麻麻的接口数据,要一个一个的去敲打成为TypeScript的inte ...

  8. 使用监听器来启动spring -------使用监听器初始化上下文参数

    问题: 数据初始化监听器要注入spring容器的对象,必须先启动spring容器才能使用监听器初始化数据. 解决: 使用监听器来启动spring框架 问题:spring框架启动需要哪些参数? 1.需要 ...

  9. Java8 新特性lambda表达式(一)初始

    本篇参考Richard Warburton的 java8 Lambdas :Functional Programming for the Masses 学习lambda表达式之前,需要知道什么是函数式 ...

  10. 吴恩达-机器学习+正则化regularization