C++ auto_ptr 被弃用了吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2404115/
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-27 23:23:18  来源:igfitidea点击:

Is auto_ptr deprecated?

c++standardssmart-pointersauto-ptrunique-ptr

提问by dimba

  1. Will auto_ptr be deprecated in incoming C++ standard?
  2. Should unique_ptr be used for ownership transfer instead of shared_ptr?
  3. If unique_ptr is not in the standard, then do I need to use shared_ptr instead?
  1. auto_ptr 在传入的 C++ 标准中会被弃用吗?
  2. 是否应该使用 unique_ptr 而不是 shared_ptr 进行所有权转让?
  3. 如果 unique_ptr 不在标准中,那么我是否需要使用 shared_ptr 代替?

采纳答案by David Rodríguez - dribeas

UPDATE: This answer was written in 2010 and as anticipated std::auto_ptrhas been deprecated. The advice is entirely valid.

更新:这个答案是在 2010 年写的,正如预期的那样std::auto_ptr已被弃用。该建议完全有效。

In C++0x std::auto_ptrwill be deprecated in favor of std::unique_ptr. The choice of smart pointer will depend on your use case and your requirements, with std::unique_ptrwith move semantics for single ownership that can be used inside containers (using move semantics) and std::shared_ptrwhen ownership is shared.

在 C++0xstd::auto_ptr中将被弃用,以支持std::unique_ptr. 智能指针的选择将取决于您的用例和您的要求,具有std::unique_ptr可在容器内使用的单一所有权的移动语义(使用移动语义)以及std::shared_ptr何时共享所有权。

You should try to use the smart pointer that best fits the situation, choosing the correct pointer type provides other programmers with insight into your design.

您应该尝试使用最适合这种情况的智能指针,选择正确的指针类型可以让其他程序员深入了解您的设计。

回答by R Samuel Klatchko

Yes, as of today auto_ptrwill be deprecated in C++0x and you should use unique_ptrinstead. From the latest draft standard (n3035), section D.9

是的,从今天起auto_ptr将在 C++0x 中被弃用,您应该使用它unique_ptr。来自最新的标准草案 (n3035),第 D.9 节

The class template auto_ptris deprecated. [ Note: The class template unique_ptr(20.9.10) provides a better solution. —end note ]

auto_ptr不推荐使用类模板。[ 注意:类模板unique_ptr(20.9.10) 提供了更好的解决方案。——尾注]

Until the standard is ratified, it's always possible that the committee will revise this decision although I feel that is unlikely for this decision.

在标准被批准之前,委员会总是有可能修改这个决定,尽管我觉得这个决定不太可能。

回答by Maxime Lorant

Not only auto_ptris deprecated in C++11 (D.10, page 1228), it will also be deletedin a future version of C++:

不仅在 C++11 (D.10, page 1228) 中auto_ptr被弃用,它也将在 C++ 的未来版本中删除

Adopted N4190, and actually removed (not just deprecated) several archaic things from the C++ standard library, including auto_ptr, bind1st/bind2nd, ptr_fun/mem_fun/mem_fun_ref, random_shuffle, and a few more. Those are now all removed from the draft C++17 standard library and will not be part of future portable C++.

采用N4190和实际删除(不只是不建议使用)几个古老的东西从C ++标准库,包括auto_ptrbind1st/ bind2ndptr_fun/ mem_fun/ mem_fun_refrandom_shuffle和几个。这些现在都已从 C++17 标准库草案中删除,并且不会成为未来可移植 C++ 的一部分。

Another document about it: Programming Language C++, Library Evolution Working Group - Document N4190, if you want more information.

关于它的另一个文档:Programming Language C++, Library Evolution Working Group - Document N4190,如果您需要更多信息。

You can convert any code using auto_ptrautomaticaly, by using unique_ptrinstead:

您可以使用auto_ptr自动转换任何代码,unique_ptr而是使用:

Any code using auto_ptrcan be mechanically converted to using unique_ptr, with move()inserted whenever auto_ptrwas being "copied".

任何代码 usingauto_ptr都可以机械地转换为 using unique_ptr,并在“复制”move()时插入auto_ptr

回答by Maxime Lorant

No, it isn't deprecated. It may be, if C++0x ever gets accepted. And it will realistically always be supported. I don't believe that any deprecated feature has ever been dropped from real-world C++ implementations.

不,它没有被弃用。如果 C++0x 被接受,它可能是。它实际上将始终得到支持。我不相信现实世界的 C++ 实现中曾经删除过任何已弃用的功能。