I am learning about smart pointers (std::auto_ptr
) and just read here and here that smart pointers (std::auto_ptr
) should not be put in containers (i.e. std::vector
) because even most compilers won't complain and it might seem correct. There is no rule that says smart pointers won't be copied internally (by vector
class for example) and transfer its ownership, then the pointer will become NULL. In the end, everything will be screwed up.
In reality, how often does this happen?
Sometimes I have vectors of pointers and if in the future I decide I want to have a vector of smart pointers what would my options?
I am aware of C++0x and Boost libraries, but for now, I would prefer to stick to a STL approach.
auto_ptr
a smart pointer :) It's pretty dumb really, and therefore most advices about smart pointers cannot (unfortunately) be applied to it (because of its screwed copy semantics). – Matthieu M.