1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
5 <META NAME="AUTHOR" CONTENT="pme@sources.redhat.com (Phil Edwards)">
6 <META NAME="KEYWORDS" CONTENT="HOWTO, libstdc++, GCC, g++, libg++, STL">
7 <META NAME="DESCRIPTION" CONTENT="HOWTO for the libstdc++ chapter 20.">
8 <META NAME="GENERATOR" CONTENT="vi and eight fingers">
9 <TITLE>libstdc++-v3 HOWTO: Chapter 20</TITLE>
10 <LINK REL=StyleSheet HREF="../lib3styles.css">
11 <!-- $Id: howto.html,v 1.1 2000/12/10 04:04:54 pme Exp $ -->
15 <H1 CLASS="centered"><A NAME="top">Chapter 20: General Utilities</A></H1>
17 <P>Chapter 20 deals with utility classes and functions, such as
18 the oft-debated <TT>auto_ptr<></TT>.
22 <!-- ####################################################### -->
26 <LI><A HREF="#1"><TT>auto_ptr</TT> is not omnipotent</A>
27 <LI><A HREF="#2"><TT>auto_ptr</TT> inside container classes</A>
28 <LI><A HREF="#3">Functors</A>
29 <LI><A HREF="#4">Pairs</A>
34 <!-- ####################################################### -->
36 <H2><A NAME="1"><TT>auto_ptr</TT> is not omnipotent</A></H2>
37 <P>I'm not going to try and explain all of the fun and delicious
38 things that can happen with misuse of the auto_ptr class template
39 (called AP here), nor am I going to try and teach you how to use
40 AP safely in the presence of copying. The AP class is a really
41 nifty idea for a smart pointer, but it is one of the dumbest of
42 all the smart pointers -- and that's fine.
44 <P>AP is not meant to be a supersmart solution to all resource
45 leaks everywhere. Neither is it meant to be an effective form
46 of garbage collection (although it can help, a little bit).
47 And it can <EM>not</EM> be used for arrays!
49 <P>AP <EM>is</EM> meant to prevent nasty leaks in the presence of
50 exceptions. That's <EM>all</EM>. This code is AP-friendly:
52 // not a recommend naming scheme, but good for web-based FAQs
53 typedef std::auto_ptr<MyClass> APMC;
55 extern function_taking_MyClass_pointer (MyClass*);
56 extern some_throwable_function ();
60 APMC ap (new MyClass(data));
62 some_throwable_function(); // this will throw an exception
64 function_taking_MyClass_pointer (ap.get());
66 </PRE>When an exception gets thrown, the instance of MyClass that's
67 been created on the heap will be <TT>delete</TT>'d as the stack is
68 unwound past <TT>func()</TT>.
70 <P>Changing that code as follows is <EM>not</EM> AP-friendly:
72 APMC ap (new MyClass[22]);
73 </PRE>You will get the same problems as you would without the use
76 char* array = new char[10]; // array new...
78 delete array; // ...but single-object delete
81 <P>AP cannot tell whether the pointer you've passed at creation points
82 to one or many things. If it points to many things, you are about
83 to die. AP is trivial to write, however, so you could write your
84 own <TT>auto_array_ptr</TT> for that situation (in fact, this has
85 been done many times; check the mailing lists, Usenet, Boost, etc).
87 <P>Return <A HREF="#top">to top of page</A> or
88 <A HREF="../faq/index.html">to the FAQ</A>.
92 <H2><A NAME="2"><TT>auto_ptr</TT> inside container classes</A></H2>
93 <P>All of the <A HREF="../23_containers/howto.html">containers</A>
94 described in the standard library require their contained types
95 to have, among other things, a copy contructor like this:
99 My_Type (My_Type const&);
102 Note the const keyword; the object being copied shouldn't change.
103 The template class <TT>auto_ptr</TT> (called AP here) does not
104 meet this requirement. Creating a new AP by copying an existing
105 one transfers ownership of the pointed-to object, which means that
106 the AP being copied must change, which in turn means that the
107 copy ctors of AP do not take const objects.
109 <P>The resulting rule is simple: <EM>Never ever use a container of
110 auto_ptr objects.</EM> The standard says that undefined behavior
111 is the result, but it is guaranteed to be messy.
113 <P>To prevent you from doing this to yourself, the
114 <A HREF="../19_diagnostics/howto.html#3">concept checks</A> built
115 in to this implementation will issue an error if you try to
116 compile code like this:
118 #include <vector>
119 #include <memory>
123 std::vector< std::auto_ptr<int> > vec_ap_int;
126 Should you try this, you will see an error along the lines of the
127 following (look through the ugly line-wrapping):
129 /installdir/include/g++-v3/bits/concept_checks.h: In
130 static member function `static _Type
131 <B>_STL_ERROR::__const_parameter_required_for_copy_constructor</B>(_Type, const
132 _Type&) [with _Type = std::auto_ptr<int>]':
133 /installdir/include/g++-v3/bits/concept_checks.h:383: passing
134 `const std::auto_ptr<int>' as `this' argument of
135 `std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>()
136 [with _Tp1 = int, _Tp = int]' discards qualifiers
138 See the highlighted error? It starts with a message in all caps
139 to get your attention.
141 <P>Return <A HREF="#top">to top of page</A> or
142 <A HREF="../faq/index.html">to the FAQ</A>.
146 <H2><A NAME="3">Functors</A></H2>
147 <P>If you don't know what functors are, you're not alone. Many people
148 get slightly the wrong idea. In the interest of not reinventing
149 the wheel, we will refer you to the introduction to the functor
150 concept written by SGI as part of their STL, in
151 <A HREF="http://www.sgi.com/Technology/STL/functors.html">their
152 http://www.sgi.com/Technology/STL/functors.html</A>.
154 <P>Return <A HREF="#top">to top of page</A> or
155 <A HREF="../faq/index.html">to the FAQ</A>.
159 <H2><A NAME="4">Pairs</A></H2>
160 <P>The <TT>pair<T1,T2></TT> is a simple and handy way to
161 carry around a pair of objects. One is of type T1, and another of
162 type T2; they may be the same type, but you don't get anything
163 extra if they are. The two members can be accessed directly, as
164 <TT>.first</TT> and <TT>.second</TT>.
166 <P>Construction is simple. The default ctor initializes each member
167 with its respective default ctor. The other simple ctor,
169 pair (const T1& x, const T2& y);
170 </PRE>does what you think it does, <TT>first</TT> getting <TT>x</TT>
171 and <TT>second</TT> getting <TT>y</TT>.
173 <P>There is a copy constructor, but it requires that your compiler
174 handle member function templates:
176 template <class U, class V> pain (const pair<U,V>& p);
177 </PRE>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 <TT>pair<T1,T2></TT>s is defined as both <TT>first</TT>
182 members comparing equal and both <TT>second</TT> members comparing
183 equal; this simply delegates responsibility to the respective
184 <TT>operator==</TT> functions (for types like MyClass) or builtin
185 comparisons (for types like int, char, etc).
187 <P>The less-than operator is a bit odd the first time you see it. It
188 is defined as evaluating to:
190 x.first < y.first ||
191 ( !(y.first < x.first) && x.second < y.second )
193 The other operators are not defined using the <TT>rel_ops</TT>
194 functions above, but their semantics are the same.
196 <P>Finally, there is a template function called <TT>make_pair</TT>
197 that takes two references-to-const objects and returns an
198 instance of a pair instantiated on their respective types:
200 pair<int,MyClass> p = make_pair(4,myobject);
203 <P>Return <A HREF="#top">to top of page</A> or
204 <A HREF="../faq/index.html">to the FAQ</A>.
210 <!-- ####################################################### -->
213 <P CLASS="fineprint"><EM>
214 Comments and suggestions are welcome, and may be sent to
215 <A HREF="mailto:pme@sources.redhat.com">Phil Edwards</A> or
216 <A HREF="mailto:gdr@gcc.gnu.org">Gabriel Dos Reis</A>.
217 <BR> $Id: howto.html,v 1.1 2000/12/10 04:04:54 pme Exp $