什么是auto_ptr_ref,它实现什么以及如何实现它 [英] what is auto_ptr_ref, what it achieves and how it achieves it

查看:201
本文介绍了什么是auto_ptr_ref,它实现什么以及如何实现它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

auto_ptr_ref文档此处说明



这是一个工具类,允许某些转换允许auto_ptr对象传递给函数并从函数返回。



有人可以解释auto_ptr_ref如何帮助实现这一点。我只想了解auto_ptr类及其内部结构。

解决方案

这很令人困惑。基本上, auto_ptr_ref 存在,因为 auto_ptr 复制构造函数并不是标准意义上的复制构造函数。



复制构造函数通常具有如下所示的签名:

  X(const X& b); 

auto_ptr 看起来像这样:

  X(X& b)

这是因为 auto_ptr 需要修改要复制的对象,以便将其指针设置为0,有时,临时表不能匹配未声明其参数的复制构造函数<$ p。

c $ c> const 。这是 auto_ptr_ref 的地方。编译器将无法调用复制构造函数的非const版本,但它可以调用转换运算符。转换运算符创建一个 auto_ptr_ref 对象,它只是指针的临时存储器。使用 auto_ptr_ref 调用 auto_ptr 构造函数或 operator =



如果您注意到, auto_ptr 中的转换运算符会自动转换为 auto_ptr_ref 在源 auto_ptr 上执行发布,就像复制构造函数。 / p>

这是一种奇怪的小舞蹈,发生在幕后,因为 auto_ptr 修改被复制的东西。 p>

关于C ++ 0x和unique_ptr的随机相关探测



在C ++ 0x, auto_ptr 已弃用,赞成 unique_ptr unique_ptr 甚至不一个复制构造函数,并使用新的'move constructor',它显式地说明它将修改对象移动并离开它在一个无用的(但仍然有效的)状态。



C ++ 0x中的move构造函数还有许多其他的好处。它使标准的STL容器存储 unique_ptr s并做正确的事情,而不是 auto_ptr s不能。它还主要消除了对swap函数的需要,因为交换函数的整体目的通常是一个从不抛出的移动构造函数或移动赋值运算符。



这是另一个期望。移动构造函数和移动赋值操作符(很像一个析构函数)从不应该抛出。


auto_ptr_ref documentation here says this

This is an instrumental class to allow certain conversions that allow auto_ptr objects to be passed to and returned from functions.

Can somebody explain how auto_ptr_ref helps in achieving this. I just want to understand the auto_ptr class and its internals

解决方案

It is rather confusing. Basically, auto_ptr_ref exists because the auto_ptr copy constructor isn't really a copy constructor in the standard sense of the word.

Copy constructors typically have a signature that looks like this:

X(const X &b);

The auto_ptr copy constructor has a signature that looks like this:

X(X &b)

This is because auto_ptr needs to modify the object being copied from in order to set its pointer to 0 to facilitate the ownership semantics of auto_ptr.

Sometimes, temporaries cannot match a copy constructor that doesn't declare its argument const. This is where auto_ptr_ref comes in. The compiler won't be able to call the non-const version of the copy constructor, but it can call the conversion operator. The conversion operator creates an auto_ptr_ref object that's just sort of a temporary holder for the pointer. The auto_ptr constructor or operator = is called with the auto_ptr_ref argument.

If you notice, the conversion operator in auto_ptr that automatically converts to an auto_ptr_ref does a release on the source auto_ptr, just like the copy constructor does.

It's kind of a weird little dance that happens behind the scenes because auto_ptr modifies the thing being copied from.

Random related tanget about C++0x and unique_ptr

In C++0x, auto_ptr is deprecated in favor of unique_ptr. unique_ptr doesn't even have a copy constructor and uses the new 'move constructor' which is explicit about the fact that it will modify the object being moved from and leave it in a useless (but still valid) state. Temporaries (aka rvalues) are explicitly always allowed to be arguments to a move constructor.

The move constructor in C++0x has a number of other big benefits. It enables the standard STL containers to store unique_ptrs and do the right thing, as opposed to how auto_ptrs cannot be. It also mostly eliminates the need for the 'swap' function as the whole purpose of the swap function is usually to be a move constructor or move assignment operator that never throws.

Which is the other expectation. The move constructor and move assignment operator (much like a destructor) are never supposed to throw.

这篇关于什么是auto_ptr_ref,它实现什么以及如何实现它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆