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]

理解smart pointer之三:unique_ptr的更多相关文章

  1. smart pointer

    smart pointer是一种abstract data type,它可以模仿指针的行为,而且额外提供了一系列诸如自己主动内存管理.边界检查等特性,这些特性是为了在保证效率的基础上降低因为对指针的不 ...

  2. C++2.0新特性(八)——<Smart Pointer(智能指针)之unique_ptr>

    一.概念介绍 unique_ptr它是一种在异常发生时可帮助避免资源泄露的smart pointer,实现了独占式拥有的概念,意味着它可确保一个对象和其他相应资源在同一时间只被一个pointer拥有, ...

  3. c++(smart pointer)

    (一)首先对智能指针有一些概念性的了解 **********本部分内容摘自开源中国社区http://my.oschina.net/u/158589/blog/28994******** 1.什么是智能 ...

  4. Smart Pointer Guidelines

    For Developers‎ > ‎ Smart Pointer Guidelines What are smart pointers? Smart pointers are a specif ...

  5. C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

    Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...

  6. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

  7. Why do we need smart pointer and how to implement it.

    Here are two simple questions. Problem A #include <string> include <iostream> using name ...

  8. c++ smart pointer

    智能指针(smart pointer)是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露.它的一种通用实现技术是使用引用计数(reference ...

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

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

随机推荐

  1. Nodejs 配置+基础

    Nodejs + NPP 配置. http://blog.csdn.net/foruok/article/details/48366765 NPM的全称是Node Package Manager,它就 ...

  2. as3中的多线程

    从fp11.4开始支持worker技术, 即as3中的线程概念, 到了fp11.5, flascc中开始支持pthread家族来创建线程. 总的来说, as3中有两种创建线程的方法: 1.直接在as3 ...

  3. linux笔记:关机重启命令shutdown,系统运行级别init,退出登录logout

    命令名称:shutdown功能:关机或重启用法:shutdown [选项] [时间]选项参数:-c 取消前一个关机命令-h 关机-r 重启时间格式:now 现在时:分 20:30其他:会正常关闭正在启 ...

  4. Facade 运行机制

    举一个常见的例子在routes.php路由文件中的Route就是使用了laravel的Facade; config/app.php 文件中的aliases数组: 再例如其中的mail: 里面只是简单的 ...

  5. 将自定义的 service provider 绑定到 IOC 容器

    首先要有一些类,可以自己自定义一些类放在app/目录下的自己新建的文件夹,在类里面实现代码逻辑 然后通过命令生成serviceprovider   (php artisan make:provider ...

  6. div布局

    Margin: Margin属性用于设置两个元素之间的距离. Padding: Padding属性用于设置一个元素的边框与其内容的距离. Clear: 使用Float属性设置一行有多个DIV后(多列) ...

  7. github for windows 桌面版使用方法

      首先得去下载github for windows,搜索下GitHub Desktop就可以找到,然后安装   打开软件进入主界面,选择右上角的设置按钮,会弹出下拉菜单,选择options   没一 ...

  8. java的HashMap与ConcurrentHashMap

    好像今天没有什么源码读,那么就来看看java的这两种HashMap有啥不一样的地方吧,在这之前先普及一下HashMap的一些基本知识: (1)放入HashMap的元素是key-value对. (2)底 ...

  9. sleep函数

    Linux下: #include <unistd.h> sleep(1); // 睡眠1秒 usleep(1); // 睡眠1微妙

  10. js字符串处理

    1.获取字符串实际长度 var jmz = {}; function strlen(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary> ...