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. reveal 1.6.3 本机破解及使用

    reveal 强大的iOS UI调试工具 1: 去官网下载最新版本:1.6.3 http://revealapp.com/download/ 2: 安装到mac上,即,解压下载的包,把app拖到mac ...

  2. 求质数算法的N种境界[1] - 试除法和初级筛法

    ★引子 前天,俺在<俺的招聘经验[4]:通过笔试答题能看出啥?>一文,以"求质数"作为例子,介绍了一些考察应聘者的经验.由于本文没有政治敏感内容,顺便就转贴到俺在CSD ...

  3. CentOS中yum安装软件时报错:No package XXX available

    yum 安装软件时,报错:No package XXX available. [root@localhost ~]# yum -y install redis Loaded plugins: fast ...

  4. 关于iOS7 设计师需要了解的十件事

    在今年的WWDC上,苹果推出了采用全新设计语言打造的iOS7.新系统弃用了诸如皮革.木质一类的伪3D拟真效果,取而代之的是更加简洁轻量的设计路线,其中文字排版成了重头戏,另外在某些方面也受到了扁平化设 ...

  5. ajax轮询session阻塞问题

    近来读了几篇关于ASP.NET下Session机制的文章,结合自己的实际应用,有点感想:        在ASP.NET的Session的默认机制下,对同一个SessionID下的用户请求ASP.NE ...

  6. poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

    题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给 ...

  7. tsne降维可视化

    Python代码:准备训练样本的数据和标签:train_X4000.txt.train_y4000.txt 放于tsne.py当前目录.(具体t-SNE – Laurens van der Maate ...

  8. C语言实现统计字符个数

    #include<stdio.h> int main() {     int sz[10]={0},zm[26]={0},z[26]={0},i,space=0,e=0,t=0;      ...

  9. HDU4815

    Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K ( ...

  10. hduacm 5104

    http://acm.hdu.edu.cn/show #include <cstdio> #include <cstring> #include <algorithm&g ...