将shared_ptr转换为auto_ptr?
我需要在代码中从shared_ptr 获取auto_ptr。我可以进行反向操作 - 将 auto_ptr 转换为共享指针,因为共享指针具有这样的构造函数:
template<class Y> explicit shared_ptr(std::auto_ptr<Y> & r);
我可以将共享指针转换为自动指针吗?还是设计上不可能?
I need to obtain auto_ptr from shared_ptr in my code. I can do reverse operation - convert auto_ptr to shared_ptr as shared_ptr has such constructor:
template<class Y> explicit shared_ptr(std::auto_ptr<Y> & r);
Can I convert shared_ptr to auto_ptr? Or it is impossible by design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这在设计上是不可能的,因为该对象可能与其他共享指针共享,因此将其“获取”到 auto_ptr 可能会导致删除引用的对象。
出于同样的原因,shared_ptr 没有像 auto_ptr 那样的“release”成员函数。
编辑:
即使shared_ptr有某种“释放”方法或允许删除其引用
在不破坏对象的情况下,它不适用于以下情况(线程 A、B):
It is impossible by design as the object may be shared with other shared pointer and thus "fetching" it to auto_ptr may lead to deleting referenced object.
For same reason shared_ptr has no "release" member function as auto_ptr.
Edit:
Even if shared_ptr had some kind of "release" method or allowed to remove its reference
without destroying the object it would not work for following case (threads A, B):
共享指针可以被很多东西共享,你不能以某种方式从它们那里获取它。这是由 Artyom 和 peoro。
一种方法是创建一个临时
auto_ptr
,并使其不再处理作用域末尾的指针。 dalle 概述了第一种方法,但这缺乏异常安全(可能会意外删除),并且它无法防止您意外地将其传递给将要转移所有权的函数(删除操作不在我们手中)。不过,我们可以创建自己的包装器来避免这种情况:
现在您可以在作用域中将
shared_ptr
视为const auto_ptr
:A shared pointer can be shared by many things, you can't just take it from them all somehow. This is elaborated by Artyom and peoro.
One approach is to make a temporary
auto_ptr
, and release it from handling the pointer at the end of the scope. dalle outlines a first approach, but this suffers from lack of exception-safety (might accidentally delete), and it cannot protect you from accidentally passing it to a function that's going to transfer ownership (where the delete falls out of our hands).We can make our own wrapper to avoid this, though:
Now you can treat a
shared_ptr
like aconst auto_ptr
, in a scope:通常这是一个坏主意,因为
auto_ptr
和shared_ptr
都拥有你的指针的所有权(根据不同的策略,它们会关心销毁它和其他东西)。让两个不同的对象持有同一指针的所有权可能会导致运行时错误,除非您出于某些非常好的(并且奇怪!)原因这样做。
Usually it's a bad idea, since both
auto_ptr
andshared_ptr
take the ownership of your pointer (they'll care about destroying it and stuff, according to different policies).Having two different objects holding the ownership for the same pointer will likely result in run time errors unless your doing it for some really good (and weird!) reasons.
假设您想要将所有权从
shared_ptr
转移到auto_ptr
,则只有当shared_ptr
的引用计数 达到时,这才可能实现> 为 1,并且shared_ptr
最初是使用自定义删除函数创建的,并且鉴于此,具体方法如下:
注意:此技术不是线程安全的。
干杯&呵呵,
Assuming you want to transfer ownership from a
shared_ptr
toauto_ptr
, this is only possible whenshared_ptr
is 1, andshared_ptr
was originally created with a custom deleter function, andGiven that, here's how:
Note: this technique isn't thread-safe.
Cheers & hth.,
您不应该这样做,因为 auto_ptr 取得了指针的所有权。
但您可以做到这一点,但请确保在超出范围之前调用
release
。编辑:上述解决方案不是异常安全的。下面的代码应该可以工作,结合一个guard类来保证
const auto_ptr
不能被复制:You should not do that, as
auto_ptr
takes ownership of the pointer.But you can do it, but make sure you call
release
before you fall out of scope.EDIT: The above solution is not exception safe. The following should work, combining a guard class with the guarantee that a
const auto_ptr
cannot be copied: