什么是 auto_ptr_ref,它实现了什么以及如何实现它
auto_ptr_ref文档这里说这个
这是一个工具类,允许某些允许将 auto_ptr 对象传递给函数并从函数返回的转换。
有人可以解释一下 auto_ptr_ref 如何帮助实现这一目标。我只想了解 auto_ptr 类及其内部结构
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是相当令人困惑的。基本上,
auto_ptr_ref
存在是因为auto_ptr
复制构造函数实际上并不是标准意义上的复制构造函数。复制构造函数通常具有如下所示的签名:
auto_ptr
复制构造函数具有如下所示的签名:这是因为
auto_ptr
需要修改从中复制的对象为了将其指针设置为 0 以方便auto_ptr
的所有权语义。有时,临时变量无法与未声明其参数
const
的复制构造函数匹配。这就是auto_ptr_ref
发挥作用的地方。编译器将无法调用复制构造函数的非常量版本,但它可以调用转换运算符。转换运算符创建一个 auto_ptr_ref 对象,它只是指针的临时持有者。auto_ptr
构造函数或operator =
使用auto_ptr_ref
参数调用。如果您注意到,
auto_ptr
中自动转换为auto_ptr_ref
的转换运算符会在源auto_ptr
上执行release
,就像复制构造函数一样。这是一种在幕后发生的奇怪的小舞蹈,因为 auto_ptr 修改了从中复制的内容。
关于 C++0x 和 unique_ptr 的随机相关 tanget
在 C++0x 中,
auto_ptr
已被弃用,取而代之的是unique_ptr
。unique_ptr
甚至没有有复制构造函数,而是使用新的“移动构造函数”,该构造函数明确表明它将修改要移出的对象并将其保留在其中无用(但仍然有效)的状态。临时变量(又名右值)始终明确允许作为移动构造函数的参数。C++0x 中的移动构造函数还有许多其他重大优点。它使标准 STL 容器能够存储
unique_ptr
并执行正确的操作,而auto_ptr 则不能这样做。它还基本上消除了对“交换”函数的需要,因为交换函数的整个目的通常是成为永远不会抛出异常的移动构造函数或移动赋值运算符。
这是另一个期望。移动构造函数和移动赋值运算符(很像析构函数)永远不应该抛出异常。
It is rather confusing. Basically,
auto_ptr_ref
exists because theauto_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:
The
auto_ptr
copy constructor has a signature that looks like this: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 ofauto_ptr
.Sometimes, temporaries cannot match a copy constructor that doesn't declare its argument
const
. This is whereauto_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 anauto_ptr_ref
object that's just sort of a temporary holder for the pointer. Theauto_ptr
constructor oroperator =
is called with theauto_ptr_ref
argument.If you notice, the conversion operator in
auto_ptr
that automatically converts to anauto_ptr_ref
does arelease
on the sourceauto_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 ofunique_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_ptr
s and do the right thing, as opposed to howauto_ptr
s 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.
我刚刚找到了一个非常好的链接和该技术的名称“移动构造函数”或“Colvin-Gibbons 技巧”
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor
I just found a very good link and a name for this technique "Move Constructors" or "Colvin-Gibbons trick"
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor
因此,可以在
auto_ptr_ref
的帮助下将智能指针存储在容器(例如 QMap)中。唯一需要的是在插入时从auto_ptr
初始化auto_ptr_ref
,并从auto_ptr_ref
返回auto_ptr
进行操作。As a result, it is possible to store smart pointers in containters (for example, QMap) with help of
auto_ptr_ref
. The only need is to initializeauto_ptr_ref
fromauto_ptr
when inserting, and backauto_ptr
fromauto_ptr_ref
to operate.