Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/tuple/doc/tuple_advanced_interface.html @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3  <head>
4    <title>Tuple library advanced features</title>
5<body bgcolor="#FFFFFF" text="#000000">
6
7<IMG SRC="../../../boost.png" 
8     ALT="C++ Boost" width="277" height="86">
9
10  </head>
11
12  <body>
13    <h1>Tuple library advanced features</h1>
14
15The advanced features described in this document are all under namespace <code>::boost::tuples</code>
16
17<h2>Metafunctions for tuple types</h2>
18<p>
19Suppose <code>T</code> is a tuple type, and <code>N</code> is a constant integral expression.
20
21<code><pre>element&lt;N, T&gt;::type</pre></code>
22
23gives the type of the <code>N</code>th element in the tuple type <code>T</code>. If <code>T</code> is const, the resulting type is const qualified as well.
24Note that the constness of <code>T</code> does not affect reference type
25elements.
26</p>
27
28<code><pre>length&lt;T&gt;::value</pre></code>
29
30gives the length of the tuple type <code>T</code>.
31</p>
32
33<h2>Cons lists</h2>
34
35<p>
36Tuples are internally represented as <i>cons lists</i>.
37For example, the tuple
38
39<code><pre>tuple&lt;A, B, C, D&gt;</pre></code>
40
41 inherits from the type
42<code><pre>cons&lt;A, cons&lt;B, cons&lt;C, cons&lt;D, null_type&gt; &gt; &gt; &gt;
43</pre></code>
44
45The tuple template provides the typedef <code>inherited</code> to access the cons list representation. E.g.:
46<code>tuple&lt;A&gt;::inherited</code> is the type <code>cons&lt;A, null_type&gt;</code>.
47</p>
48
49<h4>Empty tuple</h4>
50<p>
51The internal representation of the empty tuple <code>tuple&lt;&gt</code> is <code>null_type</code>.
52</p>
53
54<h4>Head and tail</h4>
55<p>
56Both tuple template and the cons templates provide the typedefs <code>head_type</code> and <code>tail_type</code>.
57The <code>head_type</code> typedef gives the type of the first element of the tuple (or the cons list).
58The
59<code>tail_type</code> typedef gives the remaining cons list after removing the first element.
60The head element is stored in the member variable <code>head</code> and the tail list in the member variable <code>tail</code>.
61Cons lists provide the member function <code>get_head()</code> for getting a reference to the head of a cons list, and <code>get_tail()</code> for getting a reference to the tail.
62There are const and non-const versions of both functions.
63</p>
64<p>
65Note that in a one element tuple, <code>tail_type</code> equals <code>null_type</code> and the <code>get_tail()</code> function returns an object of type <code>null_type</code>.
66</p>
67<p>
68The empty tuple (<code>null_type</code>) has no head or tail, hence the <code>get_head</code> and <code>get_tail</code> functions are not provided.
69</p>
70
71<p>
72Treating tuples as cons lists gives a convenient means to define generic functions to manipulate tuples. For example, the following pair of function templates assign 0 to each element of a tuple (obviously, the assignments must be valid operations for the element types):
73
74<pre><code>inline void set_to_zero(const null_type&amp;) {};
75
76template &lt;class H, class T&gt;
77inline void set_to_zero(cons&lt;H, T&gt;&amp; x) { x.get_head() = 0; set_to_zero(x.get_tail()); }
78</code></pre>
79<p>
80
81<h4>Constructing cons lists</h4>
82
83<p>
84A cons list can be default constructed provided that all its elements can be default constructed.
85</p>
86<p>
87A cons list can be constructed from its head and tail. The prototype of the constructor is:
88<pre><code>cons(typename access_traits&lt;head_type&gt;::parameter_type h,
89     const tail_type&amp; t)
90</code></pre>
91The traits template for the head parameter selects correct parameter types for different kinds of element types (for reference elements the parameter type equals the element type, for non-reference types the parameter type is a reference to const non-volatile element type).
92</p>
93<p>
94For a one-element cons list the tail argument (<code>null_type</code>) can be omitted.
95</p>
96
97
98<h2>Traits classes for tuple element types</h2>
99
100<h4><code>access_traits</code></h4>
101<p>
102The template <code>access_traits</code> defines three type functions. Let <code>T</code> be a type of an element in a tuple:
103<ol>
104<li><code>access_traits&lt;T&gt;::non_const_type</code> maps <code>T</code> to the return type of the non-const access functions (nonmeber and member <code>get</code> functions, and the <code>get_head</code> function).</li>
105<li><code>access_traits&lt;T&gt;::const_type</code> maps <code>T</code> to the return type of the const access functions.</li>
106<li><code>access_traits&lt;T&gt;::parameter_type</code> maps <code>T</code> to the parameter type of the tuple constructor.</li>
107</ol>
108<h4><code>make_tuple_traits</code></h4>
109
110The element types of the tuples that are created with the <code>make_tuple</code> functions are computed with the type function <code>make_tuple_traits</code>.
111The type function call <code>make_tuple_traits&lt;T&gt;::type</code> implements the following type mapping:
112<ul>
113<li><i>any reference type</i> -&gt; <i>compile time error</i>
114</li>
115<li><i>any array type</i> -&gt; <i>constant reference to the array type</i>
116</li>
117<li><code>reference_wrapper&lt;T&gt;</code> -&gt; <code>T&amp;</code>
118</li>
119<li><code>T</code> -&gt; <code>T</code>
120</li>
121</ul>
122
123Objects of type <code>reference_wrapper</code> are created with the <code>ref</code> and <code>cref</code> functions (see <A href="tuple_users_guide.html#make_tuple">The <code>make_tuple</code> function</A>.)
124</p>
125
126<p>Reference wrappers were originally part of the tuple library, but they are now a general utility of boost.
127The <code>reference_wrapper</code> template and the <code>ref</code> and <code>cref</code> functions are defined in a separate file <code>ref.hpp</code> in the main boost include directory; and directly in the <code>boost</code> namespace.
128</p>
129
130<A href="tuple_users_guide.html">Back to the user's guide</A>
131<hr>
132
133<p>&copy; Copyright Jaakko J&auml;rvi 2001.</p>
134  </body>
135</html>
Note: See TracBrowser for help on using the repository browser.