- 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?

- 53,344
- 14
- 119
- 168

- 26,717
- 34
- 141
- 196
-
FWIW, `auto_ptr` was removed in C++17. See [ISO/IEC JTC1/SC22/WG21 N4190](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm) and [Updates to my trip report : Standard C++](https://isocpp.org/blog/2014/11/updates-to-my-trip-report). – li ki Mar 16 '22 at 03:20
4 Answers
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.

- 8,165
- 2
- 13
- 35

- 204,818
- 23
- 294
- 489
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 templateunique_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.

- 74,869
- 16
- 134
- 187
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 usingunique_ptr
, withmove()
inserted wheneverauto_ptr
was being "copied".

- 34,607
- 19
- 87
- 97
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.
-
5The C++ standard has only been updated once, and that was basically just a technical corrigendum (i.e. fixed to problems that had been cited). It's not surprising that it didn't remove anything. OTOH, old features do eventually get dropped from compilers. Just for example, more C++ probably used `
` than ever used `auto_ptr`, but MS VC++ (for one) doesn't provide it anymore. – Jerry Coffin Mar 08 '10 at 20:18 -
1@Jerry iostream.h has never been part of any standard. And as such, it isn't deprecated. – Mar 08 '10 at 20:20
-
@Neil:No, but it was used a *lot*. `auto_ptr` is part of the standard, but used substantially less. From a practical viewpoint, its removal will have drastically less impact. – Jerry Coffin Mar 08 '10 at 20:23
-
2@Jerry well I use auto_ptr a lot, and don't use iostream.h at all. I sometimes think the C++ standard comitee have a bit of a bee in their collective bonnet when it comes to deprecation. Some things, like the original string streams obviously were wrong, but others like the idea of using nameless namespaces instead of the perfectly usable "static" keyword were (and are) completely bonkers. – Mar 08 '10 at 20:29
-
1@Neil:Well, let's try to put it into perspective. Regardless of what your I do personally, consider that a google search for "
" yields ~263'000 hits, and doing the same of auto_ptr gives ~66'000 hits. – Jerry Coffin Mar 08 '10 at 22:28 -
@Neil: `static` is deprecated for objects in namespace scope, presumably because (a) const objects in namespace scope are static automatically in C++, unlike C; and (b) non-const objects in namespace scope are bonkers to begin with, whatever linkage they have, but if you absolutely must have file-scoped non-const globals, unnamed namespaces do the job. – Steve Jessop Mar 08 '10 at 22:47
-
C++11 has a number of changes like the new use of the auto keyword which is incompatible with older compilers. – Richard Chambers Feb 15 '14 at 16:45
-
@SteveJessop (for posterity) Of course, the idea of making `static` deprecated was quickly reverted before C++11 was finialised, once people realised it's fine - so it's as valid as it always has been. – underscore_d Jul 13 '16 at 19:20