mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 23:14:02 +00:00
105 lines
4.3 KiB
HTML
105 lines
4.3 KiB
HTML
<html>
|
||
|
||
<head>
|
||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||
<title>Header boost/utility.hpp Documentation</title>
|
||
</head>
|
||
|
||
<body bgcolor="#FFFFFF" text="#000000">
|
||
|
||
<h1><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="center" WIDTH="277" HEIGHT="86">Header
|
||
<a href="../../boost/utility.hpp">boost/utility.hpp</a></h1>
|
||
|
||
<p>The entire contents of the header <code><a href="../../boost/utility.hpp"><boost/utility.hpp></a></code>
|
||
are in <code>namespace boost</code>.</p>
|
||
|
||
<h2>Contents</h2>
|
||
|
||
<ul>
|
||
<li>Function templates <a href="#functions next">next() and prior()</a></li>
|
||
<li>Class <a href="#Class noncopyable">noncopyable</a></li>
|
||
<li>Function template <a href="tie.html">tie()</a> and supporting class tied.</li>
|
||
</ul>
|
||
<h2> <a name="functions next">Function</a> templates next() and prior()</h2>
|
||
|
||
<p>Certain data types, such as the C++ Standard Library's forward and
|
||
bidirectional iterators, do not provide addition and subtraction via operator+()
|
||
or operator-(). This means that non-modifying computation of the next or
|
||
prior value requires a temporary, even though operator++() or operator--() is
|
||
provided. It also means that writing code like <code>itr+1</code> inside a
|
||
template restricts the iterator category to random access iterators.</p>
|
||
|
||
<p>The next() and prior() functions provide a simple way around these problems:</p>
|
||
|
||
<blockquote>
|
||
|
||
<pre>template <class T>
|
||
T next(T x) { return ++x; }
|
||
|
||
template <class X>
|
||
T prior(T x) { return --x; }</pre>
|
||
|
||
</blockquote>
|
||
|
||
<p>Usage is simple:</p>
|
||
|
||
<blockquote>
|
||
|
||
<pre>const std::list<T>::iterator p = get_some_iterator();
|
||
const std::list<T>::iterator prev = boost::prior(p);</pre>
|
||
|
||
</blockquote>
|
||
|
||
<p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>.</p>
|
||
|
||
<h2><a name="Class noncopyable">Class noncopyable</a></h2>
|
||
|
||
<p>Class <strong>noncopyable</strong> is a base class. Derive your own class from <strong>noncopyable</strong>
|
||
when you want to prohibit copy construction and copy assignment.</p>
|
||
|
||
<p>Some objects, particularly those which hold complex resources like files or
|
||
network connections, have no sensible copy semantics. Sometimes there are
|
||
possible copy semantics, but these would be of very limited usefulness and be
|
||
very difficult to implement correctly. Sometimes you're implementing a class that doesn't need to be copied
|
||
just yet and you don't want to take the time to write the appropriate functions.
|
||
Deriving from <b> noncopyable</b> will prevent the otherwise implicitly-generated
|
||
functions (which don't have the proper semantics) from becoming a trap for other programmers.</p>
|
||
|
||
<p>The traditional way to deal with these is to declare a private copy constructor and copy assignment, and then
|
||
document why this is done. But deriving from <b>noncopyable</b> is simpler
|
||
and clearer, and doesn't require additional documentation.</p>
|
||
|
||
<p>The program <a href="noncopyable_test.cpp">noncopyable_test.cpp</a> can be
|
||
used to verify class <b>noncopyable</b> works as expected. It has have been run successfully under
|
||
GCC 2.95, Metrowerks
|
||
CodeWarrior 5.0, and Microsoft Visual C++ 6.0 sp 3.</p>
|
||
|
||
<p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>.</p>
|
||
|
||
<h3>Example</h3>
|
||
<blockquote>
|
||
<pre>// inside one of your own headers ...
|
||
#include <boost/utility.hpp>
|
||
|
||
class ResourceLadenFileSystem : noncopyable {
|
||
...</pre>
|
||
</blockquote>
|
||
|
||
<h3>Rationale</h3>
|
||
<p>Class noncopyable has protected constructor and destructor members to
|
||
emphasize that it is to be used only as a base class. Dave Abrahams notes
|
||
concern about the effect on compiler optimization of adding (even trivial inline)
|
||
destructor declarations. He says "Probably this concern is misplaced, because
|
||
noncopyable will be used mostly for classes which own resources and thus have non-trivial destruction semantics."</p>
|
||
<hr>
|
||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
|
||
-->28 September, 2000<!--webbot bot="Timestamp" endspan i-checksum="39343"
|
||
-->
|
||
</p>
|
||
<p><EFBFBD> Copyright boost.org 1999. Permission to copy, use, modify, sell and
|
||
distribute this document is granted provided this copyright notice appears in
|
||
all copies. This document is provided "as is" without express or
|
||
implied warranty, and with no claim as to its suitability for any purpose.</p>
|
||
</body>
|
||
</html>
|