I heard auto_ptr is being deprecated in C++11. What is the reason for this?
Also I would like to know the difference between auto_ptr and shared_ptr.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The direct replacement for
auto_ptr(or the closest thing to one anyway) isunique_ptr. As far as the “problem” goes, it’s pretty simple:auto_ptrtransfers ownership when it’s assigned.unique_ptralso 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_ptrdoesn’t really tell you much about what it attempts to automate, whereasunique_ptris a fairly reasonable (if terse) description of what’s provided.