1 <?xml version="1.0" encoding="ISO-8859-1"?>
3 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
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" />
18 <h1 class="centered"><a name="top">Chapter 20: General Utilities</a></h1>
20 <p>Chapter 20 deals with utility classes and functions, such as
21 the oft-debated <code>auto_ptr<></code>.
25 <!-- ####################################################### -->
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>
37 <!-- ####################################################### -->
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.
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!
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:
56 // not a recommend naming scheme, but good for web-based FAQs
57 typedef std::auto_ptr<MyClass> APMC;
59 extern function_taking_MyClass_pointer (MyClass*);
60 extern some_throwable_function ();
64 APMC ap (new MyClass(data));
66 some_throwable_function(); // this will throw an exception
68 function_taking_MyClass_pointer (ap.get());
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>.
75 <p>Changing that code as follows is <em>not</em> AP-friendly:
78 APMC ap (new MyClass[22]);
80 <p>You will get the same problems as you would without the use
84 char* array = new char[10]; // array new...
86 delete array; // ...but single-object delete
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).
94 <p>Return <a href="#top">to top of page</a> or
95 <a href="../faq/index.html">to the FAQ</a>.
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:
107 My_Type (My_Type const&);
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.
117 <p>The resulting rule is simple: <em>Never ever use a container of
118 auto_ptr objects.</em> The standard says that "undefined"
119 behavior is the result, but it is guaranteed to be messy.
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:
127 #include <vector>
128 #include <memory>
132 std::vector< std::auto_ptr<int> > vec_ap_int;
135 <p>Should you try this with the checks enabled, you will see an error.
137 <p>Return <a href="#top">to top of page</a> or
138 <a href="../faq/index.html">to the FAQ</a>.
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>.
150 <p>Return <a href="#top">to top of page</a> or
151 <a href="../faq/index.html">to the FAQ</a>.
155 <h2><a name="4">Pairs</a></h2>
156 <p>The <code>pair<T1,T2></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>.
162 <p>Construction is simple. The default ctor initializes each member
163 with its respective default ctor. The other simple ctor,
166 pair (const T1& x, const T2& y);
168 <p>does what you think it does, <code>first</code> getting <code>x</code>
169 and <code>second</code> getting <code>y</code>.
171 <p>There is a copy constructor, but it requires that your compiler
172 handle member function templates:
175 template <class U, class V> pain (const pair<U,V>& p);
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.
180 <p>The comparison operators are done for you. Equality
181 of two <code>pair<T1,T2></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).
188 The less-than operator is a bit odd the first time you see it. It
189 is defined as evaluating to:
193 x.first < y.first ||
194 ( !(y.first < x.first) && x.second < y.second )
196 <p>The other operators are not defined using the <code>rel_ops</code>
197 functions above, but their semantics are the same.
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:
204 pair<int,MyClass> p = make_pair(4,myobject);
206 <p>Return <a href="#top">to top of page</a> or
207 <a href="../faq/index.html">to the FAQ</a>.
213 <!-- ####################################################### -->
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>.