tags:

views:

290

answers:

2

hi,

I heard auto_ptr is being deprecated in the new C++. What is the reason for this. Also I would like to know difference between auto_ptr vs shared_ptr

+7  A: 

shared_ptr can be stored inside containers. auto_ptr can't.

BTW unique_ptr is really the direct auto_ptr replacement, it combines the best features of both std::auto_ptr and boost::scoped_ptr.

Ben Voigt
I think you mean `unique_ptr`. I don't think `scoped_ptr` is getting standardized.
Dennis Zickefoose
yes unique and shared are getting standardised
brett
Thanks for calling me out on `unique_ptr` vs `scoped_ptr`. Fixed.
Ben Voigt
+11  A: 

The direct replacement for auto_ptr (or the closest thing to one anyway) is unique_ptr. As far as the "problem" goes, it's pretty simple: auto_ptr transfers ownership when it's assigned. unique_ptr also transfers ownership, but thanks to codification of move semantics and the magic of rvalue references, it can do so considerably more naturally. It also "fits" with the rest of the standard library considerably better (though, in fairness, some of that is thanks to the rest of the library changing to accommodate move semantics instead of always requiring copying).

The change in name is also (IMO) a welcome one -- auto_ptr doesn't really tell you much about what it attempts to automate, whereas unique_ptr is a fairly reasonable (if terse) description of what's provided.

Jerry Coffin