I learned that STL can forbid programmer putting an auto_ptr into a container. For example following code wouldn’t compile:
auto_ptr<int> a(new int(10));
vector<auto_ptr<int> > v;
v.push_back(a);
auto_ptr has the copy constructor, why this code can even compile?
Looking at the definition of
std::auto_ptr:Although there is a copy-constructor, it takes a reference to non-
const. Temporaries may not bind to this, so the type is effectively prohibited from working inside containers in any place where temporaries are used; in addition,push_backaccepts a reference toconst, so due toconst-correctness it’s impossible for the new, internal element to by copy-constructed frompush_back‘s argument.(That Wikipedia page says that “because of its copy semantics, auto_ptr may not be used in STL containers that may perform element copies in their operations”; this doesn’t mean that containers magically examine the code inside the copy constructor to decide whether it wants to make the type work as an element type. Instead, it’s just about the function signature.)
Anyway,
std::auto_ptris deprecated as of C++11 because, in the opinion of some,std::auto_ptris silly. Sorry,std::auto_ptr.