What are the advantages and disadvantages of using auto pointers (auto_ptr), compared to ordinary pointers? I’ve heard it does automatic releasing of memory but how come it is not used often?
What are the advantages and disadvantages of using auto pointers (auto_ptr), compared to ordinary
Share
The main drawback of
std::auto_ptris that it has the transfer-of-ownership semantic. That makes it impossible to storestd::auto_ptrin STL containers because the containers use the copy constructor when you store or get an element.Also, another important aspect that i have noticed about the
std::auto_ptris that they cannot serve in the use of PIMPL idiom. This is because, they require the complete definition of the wrapped class’s destructor. See this thread on c.l.c++.m for more detailed discussion.Update: Transfer of ownership
See this thread on Herb Sutter’s site for more details on what this means when used in a STL container used by STL algorithms.