You can't store an auto_ptr
itself in a std::vector
, but storing a plain pointer is fine, even if the thing it points to is an auto_ptr
.
I don't see why you'd want to do that, though. The whole point of auto_ptr
is to ensure that heap-allocated objects are deleted automatically when they go out of scope. You lose that benefit if the auto_ptr
itself has to be deleted manually.
If you want to store heap-allocated objects safely in a vector, you can use std::tr1::shared_ptr
instead of auto_ptr
, or use Boost's pointer containers. Or, if you're using C++11, std::unique_ptr
is like std::auto_ptr
except it's safe to use in containers.