这几天开始拜读侯捷先生和孟岩先生的译作《C++标准程序库:自修教程与参考手册》 。两位先生确实译功上乘,读得很顺。但是读到P55页关于auto_ptr_ref的讨论,却百思不得其解:为什么需要引入auto_ptr_ref这个辅助类呢?

从书中描述来看,仿佛与拷贝构造函数 、右值 、类型转换 有关。于是,结合auto_ptr的源代码,google之、baidu之,找了一推资料,终于初步 搞清该问题。

auto_ptr的拥有权

C++常见的智能指针有std::auto_ptr、boost::shared_ptr、boost::scoped_ptr、boost::shared_array、boost::scoped_array等。auto_ptr只是其中一种而已。但是,为什么auto_ptr才有auto_ptr_ref ,而boost::shared_ptr却没有shared_ptr_ref呢?

答案与auto_ptr的特性有关。auto_ptr强调对资源的拥有权 (ownership)。也就是说,auto_ptr是"它所指对象"的拥有者。而一个对象只能属于一个拥有者,严禁一物二主,否则就是重婚罪,意料外的灾难将随之而来。

为了保证auto_ptr的拥有权唯一,auto_ptr的拷贝构造函数和赋值操作符做了这样一件事情:移除另一个auto_ptr的拥有权 。为了说明拥有权的转移。

auto_ptr的拷贝构造函数与赋值操作符  

 /**

 *  @brief  An %auto_ptr can be constructed from another %auto_ptr.

 *  @param  a  Another %auto_ptr of the same type.

 *

 *  This object now @e owns the object previously owned by @a a,

 *  which has given up ownsership.

 */

auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) {}

/**

 *  @brief  %auto_ptr assignment operator.

 *  @param  a  Another %auto_ptr of the same type.

 *

 *  This object now @e owns the object previously owned by @a a,

 *  which has given up ownsership.  The object that this one @e

 *  used to own and track has been deleted.

 */

auto_ptr&

operator=(auto_ptr& __a) throw () {

reset(__a.release());

return *this;

}

可以看到,auto_ptr的拷贝构造函数、赋值操作符,它们的参数都是auto_ptr& ,而不是auto_ptr const & 。

一般来说,类的拷贝构造函数和赋值操作符的参数都是const &。但是auto_ptr的做法也是合理的:确保拥有权能够转移

如果auto_ptr的拷贝构造函数和赋值操作符的参数是auto_ptr const & ,那么实参的拥有权将不能转移。因为转移拥有权需要修改auto_ptr的成员变量,而实参确是一个const对象,不允许修改。

右值与const &

假设我们想写出下面的代码:

#include <iostream>

#include <memory>

using namespace std;

int main(int argc, char **argv) {

auto_ptr<int> ptr1(auto_ptr<int>(new int(1)));  //使用临时对象进行拷贝构造

auto_ptr<int> ptr2(NULL);

ptr2 = (auto_ptr<int>(new int(2)));     //使用临时对象进行赋值

}

假设没有定义auto_ptr_ref类及相关的函数,那么这段代码将不能通过编译。主要的原因是,拷贝构造函数及赋值操作符的参数:auto_ptr<int>(new int(1))和 auto_ptr<int>(new int(2)) 都是临时对象 。临时对象属于典型的右值 ,而非const &是不能指向右值的(就是说给引用赋值) (参见More
Effective C++
,Item 19)。auto_ptr的拷贝构造函数及赋值操作符的参数类型恰恰是auto_ptr&,明显 非const &。

左值和右值

左值可以出现在赋值语句的左边或右边

右值只能出现在赋值语句的右边。

例如 x*y是个右值,编译表达式x*y=10;则出现错误

非const引用不能绑定右值

普通类不会遇到这个问题,是因为他们的拷贝构造函数及赋值操作符(不管是用户定义还是编译器生成的版本),参数都是const &。

auto_ptr_ref之原理

很显然,下面的构造函数,是可以接收auto_ptr临时对象的。

auto_ptr(auto_ptr __a) throw() : _M_ptr(__a.release()) { }  //__a是传值,生成__a需要调用auto_ptr的构造函数,于是便形成了在构造函数中调用构造函数。

但另一个问题也很显然:上述构造函数不能通过编译。如果能通过编译,就会陷入循环调用。我们稍作修改:

auto_ptr(auto_ptr_ref<element_type> __ref) throw()  //element_type就是auto_ptr的模板参数。
: _M_ptr(__ref._M_ptr) { }

该版本的构造函数,可以接收auto_ptr_ref的临时对象。如果auto_ptr可以隐式转换到auto_ptr_ref,那么我们就能够用auto_ptr临时对象来调用该构造函数。这个隐式转换不难实现:

template<typename _Tp1>
operator auto_ptr_ref<_Tp1>() throw() { return auto_ptr_ref<_Tp1>(this->release()); }//将auto_ptr临时对象隐式转换为auto_ptr_ref

至此,我们可以写出下面的代码,并可以通过编译:

    #include <iostream>
#include <memory>
using namespace std; int main(int argc, char **argv) {
auto_ptr<int> ptr1(auto_ptr<int>(new int(1))); //调用auto_ptr_ref版本的构造函数
}

同理,如果我们再提供下面的函数:

    auto_ptr&
operator=(auto_ptr_ref<element_type> __ref) throw()
{
if (__ref._M_ptr != this->get())
{
delete _M_ptr;
_M_ptr = __ref._M_ptr;
}
return *this;
}

那么,下面的代码也可以通过编译:

    #include <iostream>
#include <memory>
using namespace std; int main(int argc, char **argv) {
auto_ptr<int> ptr2(NULL);
ptr2 = (auto_ptr<int>(new int(2))); //调用auto_ptr_ref版本的赋值操作符
}

auto_ptr_ref之本质

本质上,auto_ptr_ref赋予了auto_ptr“引用”的语义,这一点可以从auto_ptr_ref的注释看出:

    /**
* A wrapper class to provide auto_ptr with reference semantics.
* For example, an auto_ptr can be assigned (or constructed from)
* the result of a function which returns an auto_ptr by value.
*
* All the auto_ptr_ref stuff should happen behind the scenes.
*/
template<typename _Tp1>
struct auto_ptr_ref
{
_Tp1* _M_ptr; explicit
auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
};

因为non-const引用不能绑定到右值,所以没有从rvalue auto_ptr到 auto_ptr的转换,此时需要一个中间代理:auto_ptr_ref来完成:

rvalue auto_ptr --> auto_ptr_ref--> auto_ptr的转换。

转自:http://www.cnblogs.com/skyofbitbit/archive/2012/09/12/2681687.html

为什么需要auto_ptr_ref的更多相关文章

  1. auto_ptr源码剖析

    /* * Copyright (c) 1997-1999 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, ...

  2. C++11 auto_ptr 的问题

    auto_ptr作为最早的智能指针,可以实现以RAII手法管理堆区对象,但它设计的本意只是简单的利用C++对于栈区对象的自动析构管理堆区对象, 并不像shared_ptr那样包含引用计数,可以在每次拷 ...

  3. auto_ptr解析

    auto_ptr是当前C++标准库中提供的一种智能指针,或许相对于boost库提供的一系列眼花缭乱的智能指针, 或许相对于Loki中那个无所不包的智能指针,这个不怎么智能的智能指针难免会黯然失色.诚然 ...

  4. 【C++深入浅出】智能指针之auto_ptr学习

    起:  C++98标准加入auto_ptr,即智能指针,C++11加入shared_ptr和weak_ptr两种智能指针,先从auto_ptr的定义学习一下auto_ptr的用法. template& ...

  5. c++11 auto_ptr介绍

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

  6. auto_ptr, which can release the space automatically

    C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理. 使用std::auto_ptr,要#include <memory>.[1]  中文名 自动指针 外 ...

  7. c++智能指针《一》 auto_ptr

    转载http://www.cnblogs.com/gnagwang/archive/2010/11/19/1881811.html C++的auto_ptr auto_ptr所做的事情,就是动态分配对 ...

  8. auto_ptr 要点解析

    今天看了auto_ptr类的用法,又仔细看了看C++标准库中的符合标准的auto_ptr类别的实作,觉得自己基本上理解了auto_ptr的原理,下面就我的心得写几句,有不正确的地方,希望多多指教. 1 ...

  9. 以对象管理资源——C++智能指针auto_ptr简介

    auto_ptr是C++标准库提供的类模板,它可以帮助程序员自动管理用new表达式动态分配的单个对象.auto_ptr对象被初始化为指向由new表达式创建的对象,当auto_ptr对象的生命期结束时, ...

随机推荐

  1. 解决504 Gateway Time-out(nginx)

    504 Gateway Time-out问题常见于使用nginx作为web server的服务器的网站 我遇到这个问题是在需要插入一万多条数据时候遇到的 一般看来, 这种情况可能是由于nginx默认的 ...

  2. Android monkey介绍

    Android monkey介绍 原文地址 1 简略 monkey是android下自动化测试比较重要的的一个工具,该工具可以运行在host端或者设备(模拟器或真实设备).它会向系统发送随机事件流(即 ...

  3. MS CRM 2011的自定义和开发(11)——插件(plugin)开发(四)

    http://www.cnblogs.com/StoneGarden/archive/2012/02/08/2343294.html MS CRM 2011的自定义和开发(11)——插件(plugin ...

  4. sql 操作,

    SELECT * FROM dbo.tbl_Web_Photos AS pt LEFT JOIN dbo.tbl_Web_Friends AS fd ON pt.user_email=fd.AddEm ...

  5. UIActivityIndicatorView添加到UIButton上并响应事件

    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewSty ...

  6. MEF简单示例

    原文地址: http://www.cnblogs.com/xiaokang088/archive/2012/02/21/2361631.html MEF 的精髓在于插件式开发,方便扩展. 例如,应用程 ...

  7. c# 对话框交换数据

    本例是一个管理联系人信息的小程序,程序有两个窗体,一个主窗体,在listview控件中显示联系人信息列表,一个对话框窗体,用来显示和修改 某个联系人的信息.通过主窗体的菜单命令,可以打开对话框,并把主 ...

  8. DBA_Oracle海量数据处理分析(方法论)

    2014-12-18 Created By BaoXinjian

  9. Codeforces Round #367 (Div. 2) Hard problem

    Hard problem 题意: 有n个字符串,对第i个字符串进行反转操作代价为ci. 要使n个字符串按照字典序从小到大排列,最小的代价是多少. 题解: 反转就是reverse操作,比如说45873反 ...

  10. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...