什么是 auto_ptr_ref,它实现了什么以及如何实现它

发布于 2024-08-19 02:37:28 字数 245 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-08-26 02:37:28

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

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

X(const X &b);

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

X(X &b)

这是因为 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_ptrunique_ptr 甚至没有复制构造函数,而是使用新的“移动构造函数”,该构造函数明确表明它将修改要移出的对象并将其保留在其中无用(但仍然有效)的状态。临时变量(又名右值)始终明确允许作为移动构造函数的参数。

C++0x 中的移动构造函数还有许多其他重大优点。它使标准 STL 容器能够存储 unique_ptr 并执行正确的操作,而 auto_ptr 则不能这样做。它还基本上消除了对“交换”函数的需要,因为交换函数的整个目的通常是成为永远不会抛出异常的移动构造函数或移动赋值运算符。

这是另一个期望。移动构造函数和移动赋值运算符(很像析构函数)永远不应该抛出异常。

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.

独自唱情﹋歌 2024-08-26 02:37:28

我刚刚找到了一个非常好的链接和该技术的名称“移动构造函数”或“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

少年亿悲伤 2024-08-26 02:37:28

因此,可以在 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 initialize auto_ptr_ref from auto_ptr when inserting, and back auto_ptr from auto_ptr_ref to operate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文