C++ std::auto_ptr 到 std::unique_ptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3451099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:52:27  来源:igfitidea点击:

std::auto_ptr to std::unique_ptr

c++c++11smart-pointersauto-ptrunique-ptr

提问by Martin York

With the new standard coming (and parts already available in some compilers), the new type std::unique_ptris supposed to be a replacement for std::auto_ptr.

随着新标准的到来(以及一些编译器中已经可用的部分),新类型std::unique_ptr应该是std::auto_ptr.

Does their usage exactly overlap (so I can do a global find/replace on my code (not that I would do this, but if I did)) or should I be aware of some differences that are not apparent from reading the documentation?

它们的用法是否完全重叠(所以我可以在我的代码上进行全局查找/替换(不是我会这样做,但如果我这样做了))还是我应该知道阅读文档时不明显的一些差异?

Also if it is a direct replacement, why give it a new name rather than just improve the std::auto_ptr?

此外,如果它是直接替代品,为什么要给它一个新名称而不是仅仅改进std::auto_ptr

回答by Cubbi

You cannot do a global find/replace because you can copy an auto_ptr(with known consequences), but a unique_ptrcan only be moved. Anything that looks like

您不能进行全局查找/替换,因为您可以复制 a auto_ptr(具有已知后果),但unique_ptr只能移动 a。任何看起来像

std::auto_ptr<int> p(new int);
std::auto_ptr<int> p2 = p; 

will have to become at least like this

至少必须变成这样

std::unique_ptr<int> p(new int);
std::unique_ptr<int> p2 = std::move(p);

As for other differences, unique_ptrcan handle arrays correctly (it will call delete[], while auto_ptrwill attempt to call delete.

至于其他差异,unique_ptr可以正确处理数组(它会调用delete[],而auto_ptr会尝试调用delete.

回答by deft_code

std::auto_ptrand std::unique_ptrare incompatible in someways and a drop in replacement in others. So, no find/replace isn't good enough. However, after a find/replace working through the compile errors should fix everything except weird corner cases. Most of the compile errors will require adding a std::move.

std::auto_ptr并且std::unique_ptr在某些方面不兼容,而在其他方面则替代品下降。因此,没有查找/替换还不够好。但是,在通过编译错误进行查找/替换之后,除了奇怪的极端情况之外,应该修复所有内容。大多数编译错误都需要添加一个std::move.

  • Function scope variable:
    100% compatible, as long as you don't pass it by value to another function.
  • Return type:
    not 100% compatible but 99% compatible doesn't seem wrong.
  • Function parameter by value:
    100% compatible with one caveat, unique_ptrs must be passed through a std::movecall. This one is simple as the compiler will complain if you don't get it right.
  • Function parameter by reference:
    100% compatible.
  • Class member variable:
    This one is tricky. std::auto_ptrs copy semantics are evil. If the class disallows copying then std::unique_ptris a drop in replacement. However, if you tried to give the class reasonable copy semantics, you'll need to change the std::auto_ptrhandling code. This is simple as the compiler will complain if you don't get it right. If you allowed copying of a class with a std::auto_ptrmember withoutany special code, then shame on you and good luck.
  • 函数作用域变量:
    100% 兼容,只要您不按值将其传递给另一个函数。
  • 返回类型:
    不是 100% 兼容但 99% 兼容似乎没有错。
  • 函数参数按值:
    100% 兼容一个警告,unique_ptrs 必须通过std::move调用传递。这个很简单,因为如果你做的不对,编译器会抱怨。
  • 参考函数参数:
    100% 兼容。
  • 类成员变量:
    这个很棘手。 std::auto_ptrs 复制语义是邪恶的。如果该类不允许复制,则std::unique_ptr替换掉。但是,如果您尝试为类提供合理的复制语义,则需要更改std::auto_ptr处理代码。这很简单,因为如果你做的不对,编译器会抱怨。如果您允许在没有任何特殊代码的情况下复制具有std::auto_ptr成员的类,那么您会感到羞耻并祝您好运。

In summary, std::unique_ptris an unbroken std::auto_ptr. It disallows at compile time behaviors that were oftenerrors when using a std::auto_ptr. So if you used std::auto_ptrwith the care it needed, switching to std::unique_ptrshould be simple. If you relied on std::auto_ptr's odd behavior, then you need to refactor your code anyway.

综上所述,std::unique_ptr是一个不间断的std::auto_ptr。它不允许去考虑那些编译时的行为往往使用时的错误std::auto_ptr。因此,如果您std::auto_ptr小心使用它,切换到std::unique_ptr应该很简单。如果您依赖于std::auto_ptr的奇怪行为,那么无论如何您都需要重构您的代码。

回答by UncleBens

AFAIK, unique_ptris not a direct replacement. The major flaw that it fixes is the implicit transfer of ownership.

AFAIK,unique_ptr不是直接替代品。它修复的主要缺陷是所有权的隐式转移。

std::auto_ptr<int> a(new int(10)), b;
b = a; //implicitly transfers ownership

std::unique_ptr<int> a(new int(10)), b;
b = std::move(a); //ownership must be transferred explicitly

On the other hand, unique_ptrwill have completely new capabilities: they can be stored in containers.

另一方面,unique_ptr将具有全新的功能:它们可以存储在容器中。

回答by ValarDohaeris

Herb Sutter has a nice explanation on GotW #89:

Herb Sutter 对GotW #89有很好的解释:

What's the deal with auto_ptr?auto_ptr is most charitably characterized as a valiant attempt to create a unique_ptr before C++ had move semantics. auto_ptr is now deprecated, and should not be used in new code.

If you have auto_ptr in an existing code base, when you get a chance try doing a global search-and-replace of auto_ptr to unique_ptr; the vast majority of uses will work the same, and it might expose (as a compile-time error) or fix (silently) a bug or two you didn't know you had.

auto_ptr 有什么关系?auto_ptr 被最仁慈地描述为在 C++ 具有移动语义之前创建 unique_ptr 的勇敢尝试。auto_ptr 现在已弃用,不应在新代码中使用。

如果您在现有代码库中有 auto_ptr,当您有机会尝试将 auto_ptr 全局搜索并替换为 unique_ptr;绝大多数用途都一样,它可能会暴露(作为编译时错误)或修复(悄悄地)一两个您不知道的错误。

In other words, while a global search-and-replace may "break" your code temporarily, you should do it anyway: It may take some time to fix the compile errors, but will save you a lot more trouble in the long run.

换句话说,虽然全局搜索和替换可能会暂时“破坏”您的代码,但您无论如何都应该这样做:修复编译错误可能需要一些时间,但从长远来看会为您节省更多麻烦。