auto_ptr作为最早的智能指针,可以实现以RAII手法管理堆区对象,但它设计的本意只是简单的利用C++对于栈区对象的自动析构管理堆区对象,

并不像shared_ptr那样包含引用计数,可以在每次拷贝的时候多出一个“分身”。这时候,拷贝的语义就成了很大的问题(按理说直接禁掉可能好好些),

于是就出现了下面这个不伦不类的原型:

explicit auto_ptr (X* p=) throw();
auto_ptr (auto_ptr& a) throw();
template<class Y>
auto_ptr (auto_ptr<Y>& a) throw();
auto_ptr (auto_ptr_ref<X> r) throw();
auto_ptr& operator= (auto_ptr& a) throw();
template <class Y>
auto_ptr& operator= (auto_ptr<Y>& a) throw();
auto_ptr& operator= (auto_ptr_ref<X> r) throw();

这个跟一般我们定义一个类的拷贝(构造和赋值)函数就不一样了:

class foo
{
foo(const foo& a);
foo& operator=(const foo& a);
}

关键在于少了const,而每当auto_ptr被拷贝,它都会被置为null,相当于“移动”的语义。

这样不仅违反直觉,而且在C++11里有了正统的移动语义的情况下更显得奇怪,于是重新设计了unque_ptr

,改动不大,只是把语义纠正过来了,

default ()    

constexpr unique_ptr() noexcept;

from null pointer ()    

constexpr unique_ptr (nullptr_t) noexcept : unique_ptr() {}

from pointer ()    

explicit unique_ptr (pointer p) noexcept;

from pointer + lvalue deleter ()    

unique_ptr (pointer p,
typename conditional<is_reference<D>::value,D,const D&> del) noexcept; from pointer + rvalue deleter () unique_ptr (pointer p,
typename remove_reference<D>::type&& del) noexcept; move () unique_ptr (unique_ptr&& x) noexcept; move-cast () template <class U, class E>
unique_ptr (unique_ptr<U,E>&& x) noexcept; move from auto_ptr () template <class U>
unique_ptr (auto_ptr<U>&& x) noexcept; copy (deleted!) () unique_ptr (const unique_ptr&) = delete;

可以看到,拷贝操作直接被禁掉了。

在应用方面,auto_ptr由于奇怪的拷贝语义,导致在容器中使用的话很容易出错,比如下面的代码:

vector<auto_ptr<int>> foo;
...
auto item = foo[];

容器中的元素不知不觉就被改掉了(置为null)。

如果是unique_ptr,就看得很清楚了:

vector<unique_ptr<int>> foo;
...
auto item = std::move(foo[]);

这样也算是更改了容器,但是加上std::move之后(不加会错,因为拷贝被禁用了),代码的意图明显多了。

C++11 auto_ptr 的问题的更多相关文章

  1. c++11 auto_ptr介绍

    在代码里面看到了auto_ptr这个东西,正好以前一哥们曾经问过我这个问题..所以特意去搜了搜帖子,学习学习 http://www.cnblogs.com/gaoxianzhi/p/4451803.h ...

  2. c/c++ 标准库 智能指针( smart pointer ) 是啥玩意儿

    标准库 智能指针( smart pointer ) 是啥玩意儿 一,为什么有智能指针??? c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露. 智能指针可以帮助程序员"自 ...

  3. 读书笔记之:C++ Primer (第4版)及习题(ch12-ch18) [++++]

    读书笔记之:C++ Primer (第4版)及习题(ch12-ch18) [++++] 第12章 类 1. 类的声明与定义:前向声明,不完全类型 2. 从const函数返回*this 3. 可变数据成 ...

  4. 转载:STL四种智能指针

    转载至:https://blog.csdn.net/K346K346/article/details/81478223 STL一共给我们提供了四种智能指针: auto_ptr.unique_ptr.s ...

  5. 地区sql

    /*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...

  6. cocos2dx 中使用的一些C++ 11 特性

    0.  placeholder 头文件:<functional> namespace: placeholder placeholder 就是一堆帮助bind占参数位置的东西,名字分别为 _ ...

  7. c++ auto_ptr智能指针

    c++ auto_ptr智能指针 该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用. 使用方法: auto_ptr& ...

  8. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  9. 【转】C++11常用特性的使用经验总结

    出处 http://www.cnblogs.com/feng-sc C++11已经出来很久了,网上也早有很多优秀的C++11新特性的总结文章,在编写本博客之前,博主在工作和学习中学到的关于C++11方 ...

随机推荐

  1. Python ZIP压缩

    ru=lambda x:x.decode('u8') rp=lambda x:x.replace('\\','/') gb=lambda x:x.decode('gbk') class ZIP: de ...

  2. HTML 格式化等处理方法

    1.处理特殊字符串,清除空格,换行等 function DeleteHtml($str) { $str = trim ( $str ); // 清除字符串两边的空格 $str = preg_repla ...

  3. Balanced Binary Tree [LeetCode]

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. Discuz! X3搬家后UCenter出现UCenter info: MySQL Query Error解决方案

    Discuz! X3 X2.5论坛搬家后 登录UCenter出现报错:UCenter info: MySQL Query ErrorSQL:SELECT value FROM [Table]vars ...

  5. 转载:[AngularJS系列] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)

    来源:http://hellobug.github.io/blog/angularjs-providers/ 用AngularJS做项目,但凡用过什么service啊,factory啊,provide ...

  6. 通过php下载文件并重命名

    $filename = dirname(__FILE__) . '/oldfilename.jpg'; $out_filename = 'newfilename.jpg'; if( ! file_ex ...

  7. b/s 读取多个FTP文件(图片,视频)压缩到服务器 下载到客户端

    其实需求是这样, 要做一键导出, 有图片,有照片,youhtml,存在不同的文件夹,每次下载都必须下载最新数据,因为FTP是随时更新的. 1.这要是一直下载下载,浏览器一直跳窗口,蛋疼的我都看不下去. ...

  8. how to get soul shields in blade and soul

    These soul shields can either be obtained by E.Fleet Supply Chain or Blackram Supply Chain (4-man or ...

  9. EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(八)

    前言 本篇幅将对系统的菜单管理模块进行说明,系统的菜单采用树形结构,这样可以更好地方便层级设计和查看.本示例将说明如何通过EntityFramework读取递归的菜单树形结构,以及结合EasyUI的t ...

  10. 使用centos引导内核错误:kernel: pnp 00:0b: can't evaluate _CRS: 8

    CentOS系统在开机过程中,一直遇到黑屏提示:“kernel: pnp 00:0b: can't evaluate _CRS: 8”,不理会它仍能启动系统并正常工作,未知何故. 经查,这是内核引导的 ...