1 <section xmlns="http://docbook.org/ns/docbook" version="5.0"
2 xml:id="std.util.memory.auto_ptr" xreflabel="auto_ptr">
3 <?dbhtml filename="auto_ptr.html"?>
5 <info><title>auto_ptr</title>
7 <keyword>ISO C++</keyword>
8 <keyword>auto_ptr</keyword>
14 <section xml:id="auto_ptr.limitations"><info><title>Limitations</title></info>
17 <para>Explaining all of the fun and delicious things that can
18 happen with misuse of the <classname>auto_ptr</classname> class
19 template (called <acronym>AP</acronym> here) would take some
20 time. Suffice it to say that the use of <acronym>AP</acronym>
21 safely in the presence of copying has some subtleties.
24 The AP class is a really
25 nifty idea for a smart pointer, but it is one of the dumbest of
26 all the smart pointers -- and that's fine.
29 AP is not meant to be a supersmart solution to all resource
30 leaks everywhere. Neither is it meant to be an effective form
31 of garbage collection (although it can help, a little bit).
32 And it can <emphasis>not</emphasis>be used for arrays!
35 <acronym>AP</acronym> is meant to prevent nasty leaks in the
36 presence of exceptions. That's <emphasis>all</emphasis>. This
40 // Not a recommend naming scheme, but good for web-based FAQs.
41 typedef std::auto_ptr<MyClass> APMC;
43 extern function_taking_MyClass_pointer (MyClass*);
44 extern some_throwable_function ();
48 APMC ap (new MyClass(data));
50 some_throwable_function(); // this will throw an exception
52 function_taking_MyClass_pointer (ap.get());
55 <para>When an exception gets thrown, the instance of MyClass that's
56 been created on the heap will be <function>delete</function>'d as the stack is
57 unwound past <function>func()</function>.
59 <para>Changing that code as follows is not <acronym>AP</acronym>-friendly:
62 APMC ap (new MyClass[22]);
64 <para>You will get the same problems as you would without the use
65 of <acronym>AP</acronym>:
68 char* array = new char[10]; // array new...
70 delete array; // ...but single-object delete
73 AP cannot tell whether the pointer you've passed at creation points
74 to one or many things. If it points to many things, you are about
75 to die. AP is trivial to write, however, so you could write your
76 own <code>auto_array_ptr</code> for that situation (in fact, this has
77 been done many times; check the mailing lists, Usenet, Boost, etc).
81 <section xml:id="auto_ptr.using"><info><title>Use in Containers</title></info>
86 <para>All of the <link linkend="std.containers">containers</link>
87 described in the standard library require their contained types
88 to have, among other things, a copy constructor like this:
93 My_Type (My_Type const&);
97 Note the const keyword; the object being copied shouldn't change.
98 The template class <code>auto_ptr</code> (called AP here) does not
99 meet this requirement. Creating a new AP by copying an existing
100 one transfers ownership of the pointed-to object, which means that
101 the AP being copied must change, which in turn means that the
102 copy ctors of AP do not take const objects.
105 The resulting rule is simple: <emphasis>Never ever use a
106 container of auto_ptr objects</emphasis>. The standard says that
107 <quote>undefined</quote> behavior is the result, but it is
108 guaranteed to be messy.
111 To prevent you from doing this to yourself, the
112 <link linkend="manual.ext.compile_checks">concept checks</link> built
113 in to this implementation will issue an error if you try to
114 compile code like this:
117 #include <vector>
118 #include <memory>
122 std::vector< std::auto_ptr<int> > vec_ap_int;
126 Should you try this with the checks enabled, you will see an error.