unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一。

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.

The object is destroyed and its memory deallocated when either of the following happens:

  • unique_ptr managing the object is destroyed
  • unique_ptr managing the object is assigned another pointer via operator= or reset().

The object is destroyed using a potentially user-supplied deleter by calling Deleter(ptr). The deleter calls the destructor of the object and dispenses the memory.

A unique_ptr may also own no objects, in which case it is called empty.

There are two versions of std::unique_ptr:

  1. Manages the lifetime of a single object (e.g. allocated with new)
  2. Manages the lifetime of a dynamically-allocated array of objects (e.g. allocated with new[])

The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable. [see C++11 Concepts]

转载于:https://www.cnblogs.com/whyandinside/p/3284623.html

理解smart pointer之三:unique_ptr相关推荐

  1. 学习笔记 | c++中的smart pointer四个智能指针 shared_ptr、unique_ptr、weak_ptr、auto_ptr

    c++中的smart pointer四个智能指针 C++里面的四个智能指针: auto_ptr, shared_ptr, weak_ptr, unique_ptr其中后三个是c++11支持,并且第一个 ...

  2. 【C++】智能指针 Smart Pointer

    智能指针 智能指针 Smart Pointer auto_ptr 智能指针的自实现 shared_ptr weak_ptr unique_ptr 智能指针 Smart Pointer 用来改善传统指针 ...

  3. [C++] 什么是智能指针(Smart Pointer)以及何时使用

    答案 1 智能指针是一个类,它封装了一个原始的C++指针,以管理所指对象的生命期.没有单一的智能指针类型,但所有这些都尝试以实用的方式抽象原始指针. 智能指针应优先于原始指针. 如果你觉得你需要使用指 ...

  4. smart pointer in gamedev

    为什么80%的码农都做不了架构师?>>>    在游戏开发,确切的讲是engine开发中什么时候使用smart pointer? 现代游戏engine开发几个特点: 规模很大 效率是 ...

  5. 深入理解C指针之三:指针和函数

    深入理解C指针之三:指针和函数 原文:深入理解C指针之三:指针和函数 理解函数和指针的结合使用,需要理解程序栈.大部分现代的块结构语言,比如C,都用到了程序栈来支持函数的运行.调用函数时,会创建函数的 ...

  6. C++ smart pointer

    C++ smart pointer 什么是 smart pointer/智能指针? 1.定义 2.实现 3.应用 std::shared_ptr 转载于:https://www.cnblogs.com ...

  7. Smart Pointer

    以下内容摘自<http://blog.csdn.net/asanscape/article/details/6084600> Smart Pointer在初始化或释放等操作时,它们是一个对 ...

  8. 北京华为HCIE认证工程师快速理解Smart link-ielab实验室

    北京华为HCIE认证工程师快速理解Smart link-ielab实验室Smart Link组也叫灵活链路组,Smart link中,我们需要了解几个基本概念. 在smart link中,主接口和从接 ...

  9. 32. 对c++中的smart pointer四个智能指针shared_ptr,unique_ptr,weak_ptr,auto_ptr的理解

    C++里面的四个智能指针: auto_ptr, shared_ptr, weak_ptr, unique_ptr 其中后三个是c++11支持,并且第一个已经被11弃用. 智能指针的作用是管理一个指针, ...

最新文章

  1. 计算机科学与技术联合培养是什么意思,联合培养
  2. qt 拖拽 修改大小
  3. 五层架构(MVC+biz+lib)
  4. caany边缘检测matlab,自适应canny算法研究及其在图像边缘检测中的应用.pdf
  5. python智能光环板_学而思编程推出全新智能学习系统,搭配多种硬件
  6. 利用SqlBuikCopy实现数据批量写入
  7. 通过GitHub Actions构建和部署Jekyll网站
  8. uncode,utf8编码的文章
  9. XGBOOST从原理到实战:二分类 、多分类
  10. MySQL 分组后取最新记录
  11. 哔哩哔哩2018.9.21笔试题——扭蛋机(堆的思想解决)
  12. 现货黄金可以在手机操作吗
  13. android指南针校准 代码_android 指南针app源码(亲测可用)
  14. nyoj 455-黑色帽子
  15. 使用Docker搭建高可用Mysql数据库集群
  16. 金融数字化转型落地实践,腾讯云数据库的三问三答
  17. 基于javaweb的校园机房管理平台的设计与实现(毕业设计论文+程序源码)
  18. MATLAB处理数据,掌握这几个小技巧就够了
  19. 对于c语言排序方法的论文,c语言排序算法论文.doc
  20. 首份成绩单交出却遇重挫,被阿里对狙的贝壳还能“涨”多快?

热门文章

  1. 分享Windows Vista Beta1下载
  2. 一种绝对提高开发水平的方法
  3. [转]解读ASP.NET 5 MVC6系列(7):依赖注入
  4. 使用isql连接Sybase ASE数据库的常见错误及处理方式
  5. php实现input输入框失去焦点自动保存输入框的数据
  6. java基础之面试篇四-单向链表和双向链表区别
  7. NPM采用Rust以消除性能瓶颈
  8. 用ABAP代码读取S/4HANA生产订单工序明细
  9. 《Pro ASP.NET MVC 3 Framework》学习笔记之十三【示例项目SportsStore】
  10. IntelliJ IDEA 环境常用设置整理