TITLE: using auto_ptr in containers (Newsgroups: comp.lang.c++.moderated, 2 Mar 2000) NAGLE: John Nagle > Did the use of collections with auto_ptr elements ever get fixed, > or is that still broken? CLAMAGE: Steve Clamage The intended design of auto_ptr is to have ownership semantics. Copying an auto_ptr passes ownership of the object it points to. If auto_ptrs don't have ownership semantics, passing them to and from functions is not safe. You don't know whether to destroy the object when the auto_ptr is destroyed. The C++ Committee considered alternative designs, such as no automatic transfer of ownership plus a transfer method. These other designs were judged inferior overall. Copying an auto_ptr therefore must modify the auto_ptr itself. Since standard containers require that elements can be freely copied, you can't put a standard auto_ptr in a standard container. You can call that "broken" if you want, but the simple fact is that you cannot have both free copying of container elements and ownership semantics for auto_ptr, and still allow containers of auto_ptr. If you don't need ownership semantics, you can create your own pointer class and use it in containers. The various complaints about auto_ptr all boil down to having conflicting sets of requirements. They can't all be satisfied at once. I think a future C++ standard could have a collection of safe-pointer classes, each with different semantics. _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com