Why is it wrong to use std::auto_ptr<> with standard containers?
Why is it wrong to use std::auto_ptr<> with standard containers?
Share
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 C++ Standard says that an STL element must be ‘copy-constructible’ and ‘assignable.’ In other words, an element must be able to be assigned or copied and the two elements are logically independent.
std::auto_ptrdoes not fulfill this requirement.Take for example this code:
To overcome this limitation, you should use the
std::unique_ptr,std::shared_ptrorstd::weak_ptrsmart pointers or the boost equivalents if you don’t have C++11. Here is the boost library documentation for these smart pointers.