I was going through the auto_ptr documentation on this link auto_ptr
There is something which i could not fully understand why is it done. In the interface section there are two declarations for its copy constructor
1)
auto_ptr(auto_ptr<X>&) throw ();
2)
template <class Y>
auto_ptr(auto_ptr<Y>&) throw();
What purpose is this for.
It’s there in case you can implicitly convert the pointers:
Also, you didn’t ask but you’ll notice the copy-constructor is non-const. This is because the
auto_ptrwill take ownership of the pointer. In the sample above, afterbis constructed,dholds on to nothing. This makesauto_ptrunsuitable for use in containers, because it can’t be copied around.C++0x ditches
auto_ptrand makes one calledunique_ptr. This pointer has the same goals, but accomplishes them correctly because of move-semantics. That is, while it cannot be copied, it can “move” ownership:This makes
unique_ptrsuitable for use in containers, because they no longer copy their values, they move them.