2000-04-22 04:33:34 +08:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
2001-09-18 07:24:40 +08:00
|
|
|
<html>
|
|
|
|
<head>
|
2001-10-10 04:18:14 +08:00
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
|
|
<meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)">
|
|
|
|
<meta name="KEYWORDS" content="HOWTO, libstdc++, GCC, g++, libg++, STL">
|
|
|
|
<meta name="DESCRIPTION" content="HOWTO for the libstdc++ chapter 20.">
|
|
|
|
<meta name="GENERATOR" content="vi and eight fingers">
|
2001-09-18 07:24:40 +08:00
|
|
|
<title>libstdc++-v3 HOWTO: Chapter 20</title>
|
2001-10-12 02:41:47 +08:00
|
|
|
<link rel="StyleSheet" href="../lib3styles.css">
|
2001-09-18 07:24:40 +08:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2001-10-10 04:18:14 +08:00
|
|
|
<h1 class="centered"><a name="top">Chapter 20: General Utilities</a></h1>
|
2001-09-18 07:24:40 +08:00
|
|
|
|
|
|
|
<p>Chapter 20 deals with utility classes and functions, such as
|
|
|
|
the oft-debated <code>auto_ptr<></code>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h1>Contents</h1>
|
|
|
|
<ul>
|
|
|
|
<li><a href="#1"><code>auto_ptr</code> is not omnipotent</a>
|
|
|
|
<li><a href="#2"><code>auto_ptr</code> inside container classes</a>
|
|
|
|
<li><a href="#3">Functors</a>
|
|
|
|
<li><a href="#4">Pairs</a>
|
|
|
|
</ul>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<h2><a name="1"><code>auto_ptr</code> is not omnipotent</a></h2>
|
|
|
|
<p>I'm not going to try and explain all of the fun and delicious
|
2000-04-22 04:33:34 +08:00
|
|
|
things that can happen with misuse of the auto_ptr class template
|
|
|
|
(called AP here), nor am I going to try and teach you how to use
|
|
|
|
AP safely in the presence of copying. The AP class is a really
|
|
|
|
nifty idea for a smart pointer, but it is one of the dumbest of
|
|
|
|
all the smart pointers -- and that's fine.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>AP is not meant to be a supersmart solution to all resource
|
2000-04-22 04:33:34 +08:00
|
|
|
leaks everywhere. Neither is it meant to be an effective form
|
|
|
|
of garbage collection (although it can help, a little bit).
|
2001-09-18 07:24:40 +08:00
|
|
|
And it can <em>not</em> be used for arrays!
|
|
|
|
</p>
|
|
|
|
<p>AP <em>is</em> meant to prevent nasty leaks in the presence of
|
|
|
|
exceptions. That's <em>all</em>. This code is AP-friendly:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
// not a recommend naming scheme, but good for web-based FAQs
|
|
|
|
typedef std::auto_ptr<MyClass> APMC;
|
|
|
|
|
|
|
|
extern function_taking_MyClass_pointer (MyClass*);
|
|
|
|
extern some_throwable_function ();
|
|
|
|
|
|
|
|
void func (int data)
|
|
|
|
{
|
|
|
|
APMC ap (new MyClass(data));
|
|
|
|
|
|
|
|
some_throwable_function(); // this will throw an exception
|
|
|
|
|
|
|
|
function_taking_MyClass_pointer (ap.get());
|
|
|
|
}
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>When an exception gets thrown, the instance of MyClass that's
|
2001-09-18 07:24:40 +08:00
|
|
|
been created on the heap will be <code>delete</code>'d as the stack is
|
|
|
|
unwound past <code>func()</code>.
|
|
|
|
</p>
|
|
|
|
<p>Changing that code as follows is <em>not</em> AP-friendly:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
APMC ap (new MyClass[22]);
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>You will get the same problems as you would without the use
|
2000-04-22 04:33:34 +08:00
|
|
|
of AP:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
char* array = new char[10]; // array new...
|
|
|
|
...
|
|
|
|
delete array; // ...but single-object delete
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>AP cannot tell whether the pointer you've passed at creation points
|
2000-04-22 04:33:34 +08:00
|
|
|
to one or many things. If it points to many things, you are about
|
|
|
|
to die. AP is trivial to write, however, so you could write your
|
2001-09-18 07:24:40 +08:00
|
|
|
own <code>auto_array_ptr</code> for that situation (in fact, this has
|
2001-01-24 01:02:28 +08:00
|
|
|
been done many times; check the mailing lists, Usenet, Boost, etc).
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="2"><code>auto_ptr</code> inside container classes</a></h2>
|
|
|
|
<p>All of the <a href="../23_containers/howto.html">containers</a>
|
2001-01-24 01:02:28 +08:00
|
|
|
described in the standard library require their contained types
|
ChangeLog, [...]: Fix spelling errors.
* ChangeLog, docs/doxygen/maint.cfg.in, docs/doxygen/user.cfg.in,
docs/doxygen/doxygroups.cc, docs/doxygen/Intro.3,
docs/html/17_intro/BUGS, docs/html/17_intro/C++STYLE,
docs/html/17_intro/CHECKLIST, docs/html/17_intro/DESIGN,
docs/html/17_intro/howto.html, docs/html/17_intro/porting.html,
docs/html/17_intro/porting.texi, docs/html/18_support/howto.html,
docs/html/19_diagnostics/howto.html, docs/html/20_util/howto.html,
docs/html/21_strings/howto.html,
docs/html/23_containers/howto.html,
docs/html/26_numerics/howto.html, docs/html/27_io/howto.html,
docs/html/27_io/binary_iostreams_kuehl.txt,
docs/html/ext/sgiexts.html, docs/html/faq/index.html,
docs/html/faq/index.txt, testsuite/24_iterators/iterator.cc,
include/bits/basic_file.h, include/bits/locale_facets.h,
include/bits/locale_facets.tcc, include/bits/std_sstream.h,
include/ext/ropeimpl.h, include/ext/stl_rope.h,
libsupc++/tinfo.cc, libsupc++/cxxabi.h, libsupc++/typeinfo,
libsupc++/eh_throw.cc, acinclude.m4, aclocal.m4, configure,
configure.target, ChangeLog-2000: Fix spelling errors.
From-SVN: r47291
2001-11-24 00:29:02 +08:00
|
|
|
to have, among other things, a copy constructor like this:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2001-01-24 01:02:28 +08:00
|
|
|
struct My_Type
|
|
|
|
{
|
2001-05-31 05:55:05 +08:00
|
|
|
My_Type (My_Type const&);
|
2001-01-24 01:02:28 +08:00
|
|
|
};
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>
|
2001-01-24 01:02:28 +08:00
|
|
|
Note the const keyword; the object being copied shouldn't change.
|
2001-09-18 07:24:40 +08:00
|
|
|
The template class <code>auto_ptr</code> (called AP here) does not
|
2001-01-24 01:02:28 +08:00
|
|
|
meet this requirement. Creating a new AP by copying an existing
|
|
|
|
one transfers ownership of the pointed-to object, which means that
|
|
|
|
the AP being copied must change, which in turn means that the
|
|
|
|
copy ctors of AP do not take const objects.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The resulting rule is simple: <em>Never ever use a container of
|
2001-10-10 04:18:14 +08:00
|
|
|
auto_ptr objects.</em> The standard says that "undefined"
|
|
|
|
behavior is the result, but it is guaranteed to be messy.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>To prevent you from doing this to yourself, the
|
|
|
|
<a href="../19_diagnostics/howto.html#3">concept checks</a> built
|
2001-01-24 01:02:28 +08:00
|
|
|
in to this implementation will issue an error if you try to
|
|
|
|
compile code like this:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2001-01-24 01:02:28 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
void f()
|
|
|
|
{
|
2001-05-31 05:55:05 +08:00
|
|
|
std::vector< std::auto_ptr<int> > vec_ap_int;
|
2001-01-24 01:02:28 +08:00
|
|
|
}
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>
|
2001-05-31 05:55:05 +08:00
|
|
|
Should you try this with the checks enabled, you will see an error.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="3">Functors</a></h2>
|
|
|
|
<p>If you don't know what functors are, you're not alone. Many people
|
2000-04-22 04:33:34 +08:00
|
|
|
get slightly the wrong idea. In the interest of not reinventing
|
|
|
|
the wheel, we will refer you to the introduction to the functor
|
|
|
|
concept written by SGI as part of their STL, in
|
2002-03-07 05:22:56 +08:00
|
|
|
<a href="http://www.sgi.com/tech/stl/functors.html">their
|
|
|
|
http://www.sgi.com/tech/stl/functors.html</a>.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="4">Pairs</a></h2>
|
|
|
|
<p>The <code>pair<T1,T2></code> is a simple and handy way to
|
2000-04-22 04:33:34 +08:00
|
|
|
carry around a pair of objects. One is of type T1, and another of
|
|
|
|
type T2; they may be the same type, but you don't get anything
|
|
|
|
extra if they are. The two members can be accessed directly, as
|
2001-09-18 07:24:40 +08:00
|
|
|
<code>.first</code> and <code>.second</code>.
|
|
|
|
</p>
|
|
|
|
<p>Construction is simple. The default ctor initializes each member
|
2000-04-22 04:33:34 +08:00
|
|
|
with its respective default ctor. The other simple ctor,
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
pair (const T1& x, const T2& y);
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>does what you think it does, <code>first</code> getting <code>x</code>
|
2001-09-18 07:24:40 +08:00
|
|
|
and <code>second</code> getting <code>y</code>.
|
|
|
|
</p>
|
|
|
|
<p>There is a copy constructor, but it requires that your compiler
|
2000-04-22 04:33:34 +08:00
|
|
|
handle member function templates:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
template <class U, class V> pain (const pair<U,V>& p);
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>The compiler will convert as necessary from U to T1 and from
|
2000-04-22 04:33:34 +08:00
|
|
|
V to T2 in order to perform the respective initializations.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The comparison operators are done for you. Equality
|
|
|
|
of two <code>pair<T1,T2></code>s is defined as both <code>first</code>
|
|
|
|
members comparing equal and both <code>second</code> members comparing
|
2000-04-22 04:33:34 +08:00
|
|
|
equal; this simply delegates responsibility to the respective
|
2001-09-18 07:24:40 +08:00
|
|
|
<code>operator==</code> functions (for types like MyClass) or builtin
|
2000-04-22 04:33:34 +08:00
|
|
|
comparisons (for types like int, char, etc).
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
2001-09-26 07:51:17 +08:00
|
|
|
<a name="pairlt">
|
2001-09-18 07:24:40 +08:00
|
|
|
<p>The less-than operator is a bit odd the first time you see it. It
|
2000-04-22 04:33:34 +08:00
|
|
|
is defined as evaluating to:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
x.first < y.first ||
|
|
|
|
( !(y.first < x.first) && x.second < y.second )
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>
|
2001-09-18 07:24:40 +08:00
|
|
|
The other operators are not defined using the <code>rel_ops</code>
|
2000-04-22 04:33:34 +08:00
|
|
|
functions above, but their semantics are the same.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
2001-09-26 07:51:17 +08:00
|
|
|
</a>
|
2001-09-18 07:24:40 +08:00
|
|
|
<p>Finally, there is a template function called <code>make_pair</code>
|
2000-04-22 04:33:34 +08:00
|
|
|
that takes two references-to-const objects and returns an
|
|
|
|
instance of a pair instantiated on their respective types:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2000-04-22 04:33:34 +08:00
|
|
|
pair<int,MyClass> p = make_pair(4,myobject);
|
2001-10-10 04:18:14 +08:00
|
|
|
</pre>
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2001-10-10 04:18:14 +08:00
|
|
|
<p class="fineprint"><em>
|
2001-10-05 04:03:22 +08:00
|
|
|
See <a href="../17_intro/license.html">license.html</a> for copying conditions.
|
2000-04-22 04:33:34 +08:00
|
|
|
Comments and suggestions are welcome, and may be sent to
|
2001-10-10 04:18:14 +08:00
|
|
|
<a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
|
2001-09-18 07:24:40 +08:00
|
|
|
</em></p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
</body>
|
|
|
|
</html>
|