I’ve read today that you should not use the STL containers for auto_ptr because of
the fact that the auto_ptr deletes it rhs value in the = operator.
So i have 2 question :
1) Does this mean that all classes that have this behavior should not be used in containers?
2) what sort of containers can you use?
Yes indeed, because that is not correct copying behaviour, since the copy is not equal to the source afterwards but destroys the source. This is kind of a broken implementation of move-semantics before C++11, required for the strict unique ownership semantics of
std::auto_ptr.The real answer is actually, that classes having this behaviour (a copy constructor/assignment destroying its source) should just not exist. And fortunately this is not needed anymore nowadays, since C++11 has proper move-semantics, which realize exactly this destructive copy but in a safe way (simply said, only when the source is really not needed anymore).
Therefore
std::auto_ptris deprecated and should not be used anymore. It has been replaced bystd::unique_ptrwhich is movable but not copyable. But since C++11 containers rather move their elements than copy when appropriate, astd::unique_ptrcan be used perfectly inside of standard containers. You just cannot copy the container or fill it with a single object which would require copies ofstd::unique_ptrs, but those operations should not work anyway, since they are conceptually wrong for unique ownership semantics.And as a side note, if you actually chose
std:auto_ptrfor a reason, that is you want unique ownership semantics, then astd::shared_ptr(as suggested by other answers) is plain wrong since it exhibits shared ownership.std::unique_ptris today’sstd::auto_ptr. Never spamstd::shared_ptrs wherestd::unique_ptrs (or even raw pointers, but from your question I rule that option out) are appropriate.