views:

778

answers:

3
  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?
+11  A: 

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.

David Rodríguez - dribeas
Is `scoped_ptr` being added to C++0x? Last information I had was that it wasn't.
Michael Burr
You are right Michael. I am removing it from the answer
David Rodríguez - dribeas
+5  A: 

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.

R Samuel Klatchko
+3  A: 

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.

anon
+1 for noting that deprecated features are retained
Liz Albin
The 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 `<iostream.h>` than ever used `auto_ptr`, but MS VC++ (for one) doesn't provide it anymore.
Jerry Coffin
@Jerry iostream.h has never been part of any standard. And as such, it isn't deprecated.
anon
@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
@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.
anon
@Neil:Well, let's try to put it into perspective. Regardless of what your I do personally, consider that a google search for "<iostream.h>" yields ~263'000 hits, and doing the same of auto_ptr gives ~66'000 hits.
Jerry Coffin
@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