Thread: auto_ptrs in C++0x

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Hampton, VA

    auto_ptrs in C++0x

    Will C++0x support an STL-container of auto_ptrs?

    If not, what exactly does the STL-container of shared_ptr do (found in boost)?
    Ubuntu Desktop
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Will C++0x support an STL-container of auto_ptrs?
    No, but it almost definitely will support containers of shared_ptrs, which TR1 already supports.

    If not, what exactly does the STL-container of shared_ptr do (found in boost)?
    Allow storage of reference counted smart pointers that obey normal copying semantics in a container.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    I think C++0x will make it a compile-time error for auto_ptrs to be stored in containers that require specific copy semantics that auto_ptr doesn't adhere to. At the very least it will make it easier for library writers to enforce that.

    A shared_ptr when copied does not transfer ownership like auto_ptr and it does not copy the pointed to object. It just creates another shared reference to the object. When a container copies its elements around, you don't want ownership transferred because it might transfer to a temporary copy that is destroyed and therefore the object will be destroyed. You also don't usually want to copy the object itself, otherwise you would just store the object in the container instead of a pointer to it. By using shared_ptr, the object is never actually cleaned up until the last reference to it is destroyed, so as long as the shared_ptr is in the container the objects will always be valid.

    Boost also has ptr_vector and other pointer containers that are more specifically designed for containers that hold pointers and manage the memory themselves. Those might be preferable but I'm not sure if they will be standardized.

Popular pages Recent additions subscribe to a feed