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
Is auto_ptr deprecated?
提问by dimba
- Will auto_ptr be deprecated in incoming C++ standard?
- Should unique_ptr be used for ownership transfer instead of shared_ptr?
- If unique_ptr is not in the standard, then do I need to use shared_ptr instead?
- auto_ptr 在传入的 C++ 标准中会被弃用吗?
- 是否应该使用 unique_ptr 而不是 shared_ptr 进行所有权转让?
- 如果 unique_ptr 不在标准中,那么我是否需要使用 shared_ptr 代替?
采纳答案by David Rodríguez - dribeas
UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr
has been deprecated. The advice is entirely valid.
更新:这个答案是在 2010 年写的,正如预期的那样std::auto_ptr
已被弃用。该建议完全有效。
In C++0x std::auto_ptr
will 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_ptr
with move semantics for single ownership that can be used inside containers (using move semantics) and std::shared_ptr
when 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_ptr
will be deprecated in C++0x and you should use unique_ptr
instead. From the latest draft standard (n3035), section D.9
是的,从今天起auto_ptr
将在 C++0x 中被弃用,您应该使用它unique_ptr
。来自最新的标准草案 (n3035),第 D.9 节
The class template
auto_ptr
is deprecated. [ Note: The class templateunique_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_ptr
is 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_ptr
,bind1st
/bind2nd
,ptr_fun
/mem_fun
/mem_fun_ref
,random_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_ptr
automaticaly, by using unique_ptr
instead:
您可以使用auto_ptr
自动转换任何代码,unique_ptr
而是使用:
Any code using
auto_ptr
can be mechanically converted to usingunique_ptr
, withmove()
inserted wheneverauto_ptr
was being "copied".
任何代码 using
auto_ptr
都可以机械地转换为 usingunique_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++ 实现中曾经删除过任何已弃用的功能。