Considering the definition of std::auto_ptr :
namespace std { template <class Y> struct auto_ptr_ref {}; template <class X> class auto_ptr { public: typedef X element_type; // 20.4.5.1 construct/copy/destroy: explicit auto_ptr(X* p =0) throw(); auto_ptr(auto_ptr&) throw(); template <class Y> auto_ptr(auto_ptr<Y>&) throw(); auto_ptr& operator=(auto_ptr&) throw(); template <class Y> auto_ptr& operator=(auto_ptr<Y>&) throw(); auto_ptr& operator=(auto_ptr_ref<X>) throw(); ~auto_ptr() throw(); // 20.4.5.2 members: X& operator*() const throw(); X* operator->() const throw(); X* get() const throw(); X* release() throw(); void reset(X* p =0) throw(); // 20.4.5.3 conversions: auto_ptr(auto_ptr_ref<X>) throw(); template <class Y> operator auto_ptr_ref<Y>() throw(); template <class Y> operator auto_ptr<Y>() throw(); }; }
Although there is a copy constructor, it refers to non const . Temporary persons cannot contact this, therefore the type is actually forbidden to work inside containers anywhere where temporary resources are used; in addition, push_back accepts a reference to const , therefore, due to const correction, this is not possible for a new internal element by copying from the push_back argument.
(This Wikipedia page says that “because of its copy semantics, auto_ptr cannot be used in STL containers that can execute copies of elements in their operations”, this does not mean that containers magically scan the code inside the copy constructor to decide if it wants to make the type work as the type of the element, not just the signature of the function.)
In any case, std::auto_ptr deprecated from C ++ 11 because, according to some, std::auto_ptr stupid. Sorry, std::auto_ptr .
source share