Is auto_ptr deprecated?

C++StandardsSmart PointersAuto PtrUnique Ptr

C++ Problem Overview


  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?

C++ Solutions


Solution 1 - C++

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

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.

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.

Solution 2 - C++

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

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

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.

Solution 3 - C++

Not only auto_ptr is deprecated in C++11 (D.10, page 1228), it will also be deleted in a future version of 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++.

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

You can convert any code using auto_ptr automaticaly, by using unique_ptr instead:

> Any code using auto_ptr can be mechanically converted to using unique_ptr, with move() inserted whenever auto_ptr was being "copied".

Solution 4 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondimbaView Question on Stackoverflow
Solution 1 - C++David Rodríguez - dribeasView Answer on Stackoverflow
Solution 2 - C++R Samuel KlatchkoView Answer on Stackoverflow
Solution 3 - C++Maxime LorantView Answer on Stackoverflow
Solution 4 - C++anonView Answer on Stackoverflow