auto_ptrs in C++0x

This is a discussion on auto_ptrs in C++0x within the C++ Programming forums, part of the General Programming Boards category; 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)?...

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

    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
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21