OSDN Git Service

2002-10-07 Jonathan Wakely <jw@kayari.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / docs / html / 20_util / howto.html
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html
3           PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7 <head>
8    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
9    <meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)" />
10    <meta name="KEYWORDS" content="HOWTO, libstdc++, GCC, g++, libg++, STL" />
11    <meta name="DESCRIPTION" content="HOWTO for the libstdc++ chapter 20." />
12    <meta name="GENERATOR" content="vi and eight fingers" />
13    <title>libstdc++-v3 HOWTO:  Chapter 20</title>
14 <link rel="StyleSheet" href="../lib3styles.css" />
15 </head>
16 <body>
17
18 <h1 class="centered"><a name="top">Chapter 20:  General Utilities</a></h1>
19
20 <p>Chapter 20 deals with utility classes and functions, such as
21    the oft-debated <code>auto_ptr&lt;&gt;</code>.
22 </p>
23
24
25 <!-- ####################################################### -->
26 <hr />
27 <h1>Contents</h1>
28 <ul>
29    <li><a href="#1"><code>auto_ptr</code> is not omnipotent</a></li>
30    <li><a href="#2"><code>auto_ptr</code> inside container classes</a></li>
31    <li><a href="#3">Functors</a></li>
32    <li><a href="#4">Pairs</a></li>
33 </ul>
34
35 <hr />
36
37 <!-- ####################################################### -->
38
39 <h2><a name="1"><code>auto_ptr</code> is not omnipotent</a></h2>
40    <p>I'm not going to try and explain all of the fun and delicious
41       things that can happen with misuse of the auto_ptr class template
42       (called AP here), nor am I going to try and teach you how to use
43       AP safely in the presence of copying.  The AP class is a really
44       nifty idea for a smart pointer, but it is one of the dumbest of
45       all the smart pointers -- and that's fine.
46    </p>
47    <p>AP is not meant to be a supersmart solution to all resource
48       leaks everywhere.  Neither is it meant to be an effective form
49       of garbage collection (although it can help, a little bit).
50       And it can <em>not</em> be used for arrays!
51    </p>
52    <p>AP <em>is</em> meant to prevent nasty leaks in the presence of
53       exceptions.  That's <em>all</em>.  This code is AP-friendly:
54    </p>
55    <pre>
56     // not a recommend naming scheme, but good for web-based FAQs
57     typedef std::auto_ptr&lt;MyClass&gt;  APMC;
58
59     extern function_taking_MyClass_pointer (MyClass*);
60     extern some_throwable_function ();
61
62     void func (int data)
63     {
64         APMC  ap (new MyClass(data));
65
66         some_throwable_function();   // this will throw an exception
67
68         function_taking_MyClass_pointer (ap.get());
69     }
70    </pre>
71    <p>When an exception gets thrown, the instance of MyClass that's
72       been created on the heap will be <code>delete</code>'d as the stack is
73       unwound past <code>func()</code>.
74    </p>
75    <p>Changing that code as follows is <em>not</em> AP-friendly:
76    </p>
77    <pre>
78         APMC  ap (new MyClass[22]);
79    </pre>
80    <p>You will get the same problems as you would without the use
81       of AP:
82    </p>
83    <pre>
84         char*  array = new char[10];       // array new...
85         ...
86         delete array;                      // ...but single-object delete
87    </pre>
88    <p>AP cannot tell whether the pointer you've passed at creation points
89       to one or many things.  If it points to many things, you are about
90       to die.  AP is trivial to write, however, so you could write your
91       own <code>auto_array_ptr</code> for that situation (in fact, this has
92       been done many times; check the mailing lists, Usenet, Boost, etc).
93    </p>
94    <p>Return <a href="#top">to top of page</a> or
95       <a href="../faq/index.html">to the FAQ</a>.
96    </p>
97
98 <hr />
99 <h2><a name="2"><code>auto_ptr</code> inside container classes</a></h2>
100    <p>All of the <a href="../23_containers/howto.html">containers</a>
101       described in the standard library require their contained types
102       to have, among other things, a copy constructor like this:
103    </p>
104    <pre>
105     struct My_Type
106     {
107         My_Type (My_Type const&amp;);
108     };
109    </pre>
110    <p>Note the const keyword; the object being copied shouldn't change.
111       The template class <code>auto_ptr</code> (called AP here) does not
112       meet this requirement.  Creating a new AP by copying an existing
113       one transfers ownership of the pointed-to object, which means that
114       the AP being copied must change, which in turn means that the
115       copy ctors of AP do not take const objects.
116    </p>
117    <p>The resulting rule is simple:  <em>Never ever use a container of
118       auto_ptr objects.</em>  The standard says that &quot;undefined&quot;
119       behavior is the result, but it is guaranteed to be messy.
120    </p>
121    <p>To prevent you from doing this to yourself, the
122       <a href="../19_diagnostics/howto.html#3">concept checks</a> built
123       in to this implementation will issue an error if you try to
124       compile code like this:
125    </p>
126    <pre>
127     #include &lt;vector&gt;
128     #include &lt;memory&gt;
129     
130     void f()
131     {
132         std::vector&lt; std::auto_ptr&lt;int&gt; &gt;   vec_ap_int;
133     }
134    </pre>
135    <p>Should you try this with the checks enabled, you will see an error.
136    </p>
137    <p>Return <a href="#top">to top of page</a> or
138       <a href="../faq/index.html">to the FAQ</a>.
139    </p>
140
141 <hr />
142 <h2><a name="3">Functors</a></h2>
143    <p>If you don't know what functors are, you're not alone.  Many people
144       get slightly the wrong idea.  In the interest of not reinventing
145       the wheel, we will refer you to the introduction to the functor
146       concept written by SGI as part of their STL, in
147       <a href="http://www.sgi.com/tech/stl/functors.html">their
148       http://www.sgi.com/tech/stl/functors.html</a>.
149    </p>
150    <p>Return <a href="#top">to top of page</a> or
151       <a href="../faq/index.html">to the FAQ</a>.
152    </p>
153
154 <hr />
155 <h2><a name="4">Pairs</a></h2>
156    <p>The <code>pair&lt;T1,T2&gt;</code> is a simple and handy way to
157       carry around a pair of objects.  One is of type T1, and another of
158       type T2; they may be the same type, but you don't get anything
159       extra if they are.  The two members can be accessed directly, as
160       <code>.first</code> and <code>.second</code>.
161    </p>
162    <p>Construction is simple.  The default ctor initializes each member
163       with its respective default ctor.  The other simple ctor,
164    </p>
165    <pre>
166     pair (const T1&amp; x, const T2&amp; y);
167    </pre>
168    <p>does what you think it does, <code>first</code> getting <code>x</code>
169       and <code>second</code> getting <code>y</code>.
170    </p>
171    <p>There is a copy constructor, but it requires that your compiler
172       handle member function templates:
173    </p>
174    <pre>
175     template &lt;class U, class V&gt; pain (const pair&lt;U,V&gt;&amp; p);
176    </pre>
177    <p>The compiler will convert as necessary from U to T1 and from
178       V to T2 in order to perform the respective initializations.
179    </p>
180    <p>The comparison operators are done for you.  Equality
181       of two <code>pair&lt;T1,T2&gt;</code>s is defined as both <code>first</code>
182       members comparing equal and both <code>second</code> members comparing
183       equal; this simply delegates responsibility to the respective
184       <code>operator==</code> functions (for types like MyClass) or builtin
185       comparisons (for types like int, char, etc).
186    </p>
187    <p><a name="pairlt">
188       The less-than operator is a bit odd the first time you see it.  It
189       is defined as evaluating to:
190       </a>
191    </p>
192    <pre>
193     x.first  &lt;  y.first  ||
194         ( !(y.first  &lt;  x.first)  &amp;&amp;  x.second  &lt;  y.second )
195    </pre>
196    <p>The other operators are not defined using the <code>rel_ops</code>
197       functions above, but their semantics are the same.
198    </p>
199    <p>Finally, there is a template function called <code>make_pair</code>
200       that takes two references-to-const objects and returns an
201       instance of a pair instantiated on their respective types:
202    </p>
203    <pre>
204     pair&lt;int,MyClass&gt; p = make_pair(4,myobject);
205    </pre>
206    <p>Return <a href="#top">to top of page</a> or
207       <a href="../faq/index.html">to the FAQ</a>.
208    </p>
209
210
211
212
213 <!-- ####################################################### -->
214
215 <hr />
216 <p class="fineprint"><em>
217 See <a href="../17_intro/license.html">license.html</a> for copying conditions.
218 Comments and suggestions are welcome, and may be sent to
219 <a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
220 </em></p>
221
222
223 </body>
224 </html>