]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.7/doc/xml/manual/auto_ptr.xml
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.7 / doc / xml / manual / auto_ptr.xml
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"?>
4
5 <info><title>auto_ptr</title>
6   <keywordset>
7     <keyword>
8       ISO C++
9     </keyword>
10     <keyword>
11       auto_ptr
12     </keyword>
13   </keywordset>
14 </info>
15
16
17
18 <section xml:id="auto_ptr.limitations"><info><title>Limitations</title></info>
19
20
21    <para>Explaining all of the fun and delicious things that can
22    happen with misuse of the <classname>auto_ptr</classname> class
23    template (called <acronym>AP</acronym> here) would take some
24    time. Suffice it to say that the use of <acronym>AP</acronym>
25    safely in the presence of copying has some subtleties.
26    </para>
27    <para>
28      The AP class is a really
29       nifty idea for a smart pointer, but it is one of the dumbest of
30       all the smart pointers -- and that's fine.
31    </para>
32    <para>
33      AP is not meant to be a supersmart solution to all resource
34       leaks everywhere.  Neither is it meant to be an effective form
35       of garbage collection (although it can help, a little bit).
36       And it can <emphasis>not</emphasis>be used for arrays!
37    </para>
38    <para>
39      <acronym>AP</acronym> is meant to prevent nasty leaks in the
40      presence of exceptions.  That's <emphasis>all</emphasis>.  This
41      code is AP-friendly:
42    </para>
43    <programlisting>
44     // Not a recommend naming scheme, but good for web-based FAQs.
45     typedef std::auto_ptr&lt;MyClass&gt;  APMC;
46
47     extern function_taking_MyClass_pointer (MyClass*);
48     extern some_throwable_function ();
49
50     void func (int data)
51     {
52         APMC  ap (new MyClass(data));
53
54         some_throwable_function();   // this will throw an exception
55
56         function_taking_MyClass_pointer (ap.get());
57     }
58    </programlisting>
59    <para>When an exception gets thrown, the instance of MyClass that's
60       been created on the heap will be <function>delete</function>'d as the stack is
61       unwound past <function>func()</function>.
62    </para>
63    <para>Changing that code as follows is not <acronym>AP</acronym>-friendly:
64    </para>
65    <programlisting>
66         APMC  ap (new MyClass[22]);
67    </programlisting>
68    <para>You will get the same problems as you would without the use
69       of <acronym>AP</acronym>:
70    </para>
71    <programlisting>
72         char*  array = new char[10];       // array new...
73         ...
74         delete array;                      // ...but single-object delete
75    </programlisting>
76    <para>
77      AP cannot tell whether the pointer you've passed at creation points
78       to one or many things.  If it points to many things, you are about
79       to die.  AP is trivial to write, however, so you could write your
80       own <code>auto_array_ptr</code> for that situation (in fact, this has
81       been done many times; check the mailing lists, Usenet, Boost, etc).
82    </para>
83 </section>
84
85 <section xml:id="auto_ptr.using"><info><title>Use in Containers</title></info>
86
87
88   <para>
89   </para>
90   <para>All of the <link linkend="std.containers">containers</link>
91       described in the standard library require their contained types
92       to have, among other things, a copy constructor like this:
93   </para>
94    <programlisting>
95     struct My_Type
96     {
97         My_Type (My_Type const&amp;);
98     };
99    </programlisting>
100    <para>
101      Note the const keyword; the object being copied shouldn't change.
102      The template class <code>auto_ptr</code> (called AP here) does not
103      meet this requirement.  Creating a new AP by copying an existing
104      one transfers ownership of the pointed-to object, which means that
105      the AP being copied must change, which in turn means that the
106      copy ctors of AP do not take const objects.
107    </para>
108    <para>
109      The resulting rule is simple: <emphasis>Never ever use a
110      container of auto_ptr objects</emphasis>. The standard says that
111      <quote>undefined</quote> behavior is the result, but it is
112      guaranteed to be messy.
113    </para>
114    <para>
115      To prevent you from doing this to yourself, the
116       <link linkend="manual.ext.compile_checks">concept checks</link> built
117       in to this implementation will issue an error if you try to
118       compile code like this:
119    </para>
120    <programlisting>
121     #include &lt;vector&gt;
122     #include &lt;memory&gt;
123
124     void f()
125     {
126         std::vector&lt; std::auto_ptr&lt;int&gt; &gt;   vec_ap_int;
127     }
128    </programlisting>
129    <para>
130 Should you try this with the checks enabled, you will see an error.
131    </para>
132 </section>
133
134 </section>