Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/utility/utility.htm @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

File size: 9.7 KB
Line 
1<html>
2        <head>
3                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4                <title>Header boost/utility.hpp Documentation</title>
5        </head>
6        <body bgcolor="#FFFFFF" text="#000000">
7                <h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" WIDTH="277" HEIGHT="86">Header
8                        <a href="../../boost/utility.hpp">boost/utility.hpp</a></h1>
9                <p>The entire contents of the header <code><a href="../../boost/utility.hpp">&lt;boost/utility.hpp&gt;</a></code>
10                        are in <code>namespace boost</code>.</p>
11                <h2>Contents</h2>
12                <ul>
13                        <li>
14                                Class templates supporting the <a href="base_from_member.html">base-from-member
15                                        idiom</a></li>
16                        <li>
17                                Function templates <a href="#checked_delete">checked_delete() and
18                                        checked_array_delete()</a></li>
19                        <li>
20                                Function templates <a href="#functions_next_prior">next() and prior()</a></li>
21                        <li>
22                                Class <a href="#Class_noncopyable">noncopyable</a></li>
23                        <li>
24                                Function template <a href="#addressof">addressof()</a></li>
25                        <li>Class template <a href="#result_of">result_of</a></li>
26                </ul>
27                <h2>
28                        Function templates <a name="checked_delete">checked_delete</a>() and
29                        checked_array_delete()</h2>
30                <p>See <a href="checked_delete.html">separate documentation</a>.</p>
31                <h2>
32                        <a name="functions_next_prior">Function</a> templates next() and prior()</h2>
33                <p>Certain data types, such as the C++ Standard Library's forward and bidirectional
34                        iterators, do not provide addition and subtraction via operator+() or
35                        operator-().&nbsp; This means that non-modifying computation of the next or
36                        prior value requires a temporary, even though operator++() or operator--() is
37                        provided.&nbsp; It also means that writing code like <code>itr+1</code> inside
38                        a template restricts the iterator category to random access iterators.</p>
39                <p>The next() and prior() functions provide a simple way around these problems:</p>
40                <blockquote>
41                        <pre>template &lt;class T&gt;
42T next(T x) { return ++x; }
43
44template &lt;class T, class Distance&gt;
45T next(T x, Distance n)
46{
47    std::advance(x, n);
48    return x;
49}
50
51template &lt;class T&gt;
52T prior(T x) { return --x; }
53
54template &lt;class T, class Distance&gt;
55T prior(T x, Distance n)
56{
57    std::advance(x, -n);
58    return x;
59}</pre>
60                </blockquote>
61                <p>Usage is simple:</p>
62                <blockquote>
63                        <pre>const std::list&lt;T&gt;::iterator p = get_some_iterator();
64const std::list&lt;T&gt;::iterator prev = boost::prior(p);
65const std::list&lt;T&gt;::iterator next = boost::next(prev, 2);</pre>
66                </blockquote>
67                <p>The distance from the given iterator should be supplied as an absolute value. For
68                        example, the iterator four iterators prior to the given iterator <code>p</code>
69                        may be obtained by <code>prior(p, 4)</code>.</p>
70                <p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>.  Two-argument versions by Daniel Walker.</p>
71                <h2><a name="Class_noncopyable">Class noncopyable</a></h2>
72                <p>Class <strong>noncopyable</strong> is a base class.&nbsp; Derive your own class
73                        from <strong>noncopyable</strong> when you want to prohibit copy construction
74                        and copy assignment.</p>
75                <p>Some objects, particularly those which hold complex resources like files or
76                        network connections, have no sensible copy semantics.&nbsp; Sometimes there are
77                        possible copy semantics, but these would be of very limited usefulness and be
78                        very difficult to implement correctly.&nbsp; Sometimes you're implementing a
79                        class that doesn't need to be copied just yet and you don't want to take the
80                        time to write the appropriate functions.&nbsp; Deriving from <b>noncopyable</b> 
81                        will prevent the otherwise implicitly-generated functions (which don't have the
82                        proper semantics) from becoming a trap for other programmers.</p>
83                <p>The traditional way to deal with these is to declare a private copy constructor
84                        and copy assignment, and then document why this is done.&nbsp; But deriving
85                        from <b>noncopyable</b> is simpler and clearer, and doesn't require additional
86                        documentation.</p>
87                <p>The program <a href="noncopyable_test.cpp">noncopyable_test.cpp</a> can be used
88                        to verify class <b>noncopyable</b> works as expected. It has have been run
89                        successfully under GCC 2.95, Metrowerks CodeWarrior 5.0, and Microsoft Visual
90                        C++ 6.0 sp 3.</p>
91                <p>Contributed by <a href="../../people/dave_abrahams.htm">Dave Abrahams</a>.</p>
92                <h3>Example</h3>
93                <blockquote>
94                        <pre>// inside one of your own headers ...
95#include &lt;boost/utility.hpp&gt;
96
97class ResourceLadenFileSystem : boost::noncopyable {
98...</pre>
99                </blockquote>
100                <h3>Rationale</h3>
101                <p>Class noncopyable has protected constructor and destructor members to emphasize
102                        that it is to be used only as a base class.&nbsp; Dave Abrahams notes concern
103                        about the effect on compiler optimization of adding (even trivial inline)
104                        destructor declarations. He says &quot;Probably this concern is misplaced,
105                        because noncopyable will be used mostly for classes which own resources and
106                        thus have non-trivial destruction semantics.&quot;</p>
107                <h2><a name="addressof">Function template addressof()</a></h2>
108                <p>Function <strong>addressof()</strong> returns the address of an object.</p>
109                <blockquote>
110                        <pre>template &lt;typename T&gt; inline T*                addressof(T& v);
111template &lt;typename T&gt; inline const T*          addressof(const T& v);
112template &lt;typename T&gt; inline volatile T*       addressof(volatile T& v);
113template &lt;typename T&gt; inline const volatile T* addressof(const volatile T& v);
114</pre>
115                </blockquote>
116                <p>C++ allows programmers to replace the unary <strong>operator&()</strong> class
117                        member used to get the address of an object. Getting the real address of an
118                        object requires ugly casting tricks to avoid invoking the overloaded <strong>operator&()</strong>.
119                        Function <strong>addressof()</strong> provides a wrapper around the necessary
120                        code to make it easy to get an object's real address.
121                </p>
122                <p>The program <a href="addressof_test.cpp">addressof_test.cpp</a> can be used to
123                        verify that <b>addressof()</b> works as expected.</p>
124                <p>Contributed by Brad King based on ideas from discussion with Doug Gregor.</p>
125                <h3>Example</h3>
126                <blockquote>
127                        <pre>#include &lt;boost/utility.hpp&gt;
128
129struct useless_type {};
130class nonaddressable {
131  useless_type operator&() const;
132};
133
134void f() {
135  nonaddressable x;
136  nonaddressable* xp = boost::addressof(x);
137  // nonaddressable* xpe = &amp;x; /* error */
138}</pre>
139                </blockquote>
140                <h2><a name="result_of">Class template
141                result_of</a></h2> <p>The class template
142                <code>result_of</code> helps determine the type of a
143                call expression. Given an lvalue <code>f</code> of
144                type <code>F</code> and lvalues <code>t1</code>,
145                <code>t2</code>, ..., <code>t<em>N</em></code> of
146                types <code>T1</code>, <code>T2</code>, ...,
147                <code>T<em>N</em></code>, respectively, the type
148                <code>result_of&lt;F(T1, T2, ...,
149                T<em>N</em>)&gt;::type</code> defines the result type
150                of the expression <code>f(t1, t2,
151                ...,t<em>N</em>)</code>. The implementation permits
152                the type <code>F</code> to be a function pointer,
153                function reference, member function pointer, or class
154                type. When <code>F</code> is a class type with a
155                member type <code>result_type</code>,
156                <code>result_of&lt;F(T1, T2, ...,
157                T<em>N</em>)&gt;</code> is
158                <code>F::result_type</code>. Otherwise,
159                <code>result_of&lt;F(T1, T2, ...,
160                T<em>N</em>)&gt;</code> is <code>F::result&lt;F(T1,
161                T2, ..., T<em>N</em>)&gt;::type</code> when
162                <code><em>N</em> &gt; 0</code> or <code>void</code>
163                when <code><em>N</em> = 0</code>. For additional
164                information about <code>result_of</code>, see the
165                current draft of the C++ Library TR, <a
166                href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2004/n1647.pdf">N1647</a>,
167                or the <code>result_of</code> <a
168                href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1454.html">proposal</a>.</p>
169
170                <p>Class template <code>result_of</code> resides in
171                the header <code>&lt;<a
172                href="../../boost/utility/result_of.hpp">boost/utility/result_of.hpp</a>&gt;</code>. By
173                default, <em>N</em> may be any value between 0 and
174                10. To change the upper limit, define the macro
175                <code>BOOST_RESULT_OF_NUM_ARGS</code> to the maximum
176                value for <em>N</em>.</p>
177
178                <a name="BOOST_NO_RESULT_OF"></a>
179                <p>This implementation of <code>result_of</code> requires class template partial specialization, the ability to parse function types properly, and support for SFINAE. If <code>result_of</code> is not supported by your compiler, including the header <code>boost/utility/result_of.hpp</code> will define the macro <code>BOOST_NO_RESULT_OF</code>. Contributed by Doug Gregor.</p>
180
181                <h2>Class templates for the Base-from-Member Idiom</h2>
182                <p>See <a href="base_from_member.html">separate documentation</a>.</p>
183                <hr>
184                <p>Revised&nbsp; <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan
185-->02 May, 2004<!--webbot bot="Timestamp" endspan i-checksum="38582"
186-->
187                </p>
188                <p>&copy; Copyright boost.org 1999-2003. Permission to copy, use, modify, sell and distribute
189                        this document is granted provided this copyright notice appears in all copies.
190                        This document is provided &quot;as is&quot; without express or implied
191                        warranty, and with no claim as to its suitability for any purpose.</p>
192        </body>
193</html>
Note: See TracBrowser for help on using the repository browser.