File: placeholders.html

package info (click to toggle)
boost 1.34.1-14
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 116,412 kB
  • ctags: 259,566
  • sloc: cpp: 642,395; xml: 56,450; python: 17,612; ansic: 14,520; sh: 2,265; yacc: 858; perl: 481; makefile: 478; lex: 94; sql: 74; csh: 6
file content (165 lines) | stat: -rw-r--r-- 8,086 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
<title>THE BOOST MPL LIBRARY: Placeholders</title>
<link rel="stylesheet" href="../style.css" type="text/css" />
</head>
<body class="docframe">
<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Prev</a>&nbsp;<a href="./placeholder-expression.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./placeholder-expression.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
<td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./tutorial-metafunctions.html" class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <a href="./lambda-details.html" class="navigation-link">Lambda Details</a> / <a href="./placeholders.html" class="navigation-link">Placeholders</a></td>
</tr></table><div class="header-separator"></div>
<div class="section" id="placeholders">
<h1><a class="toc-backref" href="./lambda-details.html#id55" name="placeholders">Placeholders</a></h1>
<p>The definition of &quot;placeholder&quot; may surprise you:</p>
<div class="admonition-definition admonition">
<p class="admonition-title first">Definition</p>
<p>A <strong>placeholder</strong> is a metafunction class of the form <tt class="literal"><span class="pre">mpl::arg&lt;X&gt;</span></tt>.</p>
</div>
<div class="section" id="implementation">
<h2><a name="implementation">Implementation</a></h2>
<p>The convenient names <tt class="literal"><span class="pre">_1</span></tt>, <tt class="literal"><span class="pre">_2</span></tt>,... <tt class="literal"><span class="pre">_5</span></tt> are actually
<tt class="literal"><span class="pre">typedef</span></tt>s for specializations of <tt class="literal"><span class="pre">mpl::arg</span></tt> that simply
select the <em>N</em>th argument for any <em>N</em>. <a class="footnote-reference" href="#config" id="id12" name="id12">[6]</a>  The
implementation of placeholders looks something like this:</p>
<table class="footnote" frame="void" id="config" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id12" name="config">[6]</a></td><td>MPL provides five placeholders by default.  See
the Configuration Macros section of <a class="reference" href="./reference-manual.html">the MPL reference manual</a> for a
description of how to change the number of placeholders
provided.</td></tr>
</tbody>
</table>
<pre class="literal-block">
namespace boost { namespace mpl { namespace placeholders {

template &lt;int N&gt; struct arg; // forward declarations
struct void_;

template &lt;&gt;
struct arg&lt;<strong>1</strong>&gt;
{
    template &lt;
      class <strong>A1</strong>, class A2 = void_, ... class A<em>m</em> = void_&gt;
    struct apply
    {
        typedef <strong>A1</strong> type; // return the first argument
    };
};
typedef <strong>arg&lt;1&gt; _1</strong>;

template &lt;&gt;
struct arg&lt;<strong>2</strong>&gt;
{
    template &lt;
      class A1, class <strong>A2</strong>, class A3 = void_, ...class A<em>m</em> = void_
    &gt;
    struct apply
    {
        typedef <strong>A2</strong> type; // return the second argument
    };
};
typedef <strong>arg&lt;2&gt; _2</strong>;

<em>more specializations and typedefs...</em>

}}}
</pre>
<!-- @example.replace('...','') -->
<p>Remember that invoking a metafunction class is the same as invoking
its nested <tt class="literal"><span class="pre">apply</span></tt> metafunction.  When a placeholder in a lambda
expression is evaluated, it is invoked on the expression's actual
arguments, returning just one of them.  The results are then
substituted back into the lambda expression and the evaluation
process continues.</p>
</div>
<div class="section" id="the-unnamed-placeholder">
<h2><a name="the-unnamed-placeholder">The Unnamed Placeholder</a></h2>
<p>There's one special placeholder, known as the <strong>unnamed
placeholder</strong>, that we haven't yet defined:</p>
<pre class="literal-block">
namespace boost { namespace mpl { namespace placeholders {

<strong>typedef arg&lt;-1&gt; _;</strong> // the unnamed placeholder

}}}
</pre>
<!-- @ stack[-2].prepend('namespace shield {') 
example.append('}') # so we don't conflict with the prefix
compile('all') -->
<p>The details of its implementation aren't important; all you really
need to know about the unnamed placeholder is that it gets special
treatment.  When a lambda expression is being transformed into a
metafunction class by <tt class="literal"><span class="pre">mpl::lambda</span></tt>,</p>
<blockquote>
the <em>n</em>th appearance of the unnamed placeholder <em>in a given
template specialization</em> is replaced with <tt class="literal"><span class="pre">_</span></tt><em>n</em>.</blockquote>
<p>So, for example, every row of Table 1.1
below contains two equivalent lambda expressions.</p>
<table border="1" class="table">
<caption>Unnamed Placeholder Semantics</caption>
<colgroup>
<col width="48%" />
<col width="52%" />
</colgroup>
<tbody valign="top">
<tr><td><pre class="first last literal-block">
mpl::plus&lt;_,_&gt;
</pre>
</td>
<td><pre class="first last literal-block">
mpl::plus&lt;_1,_2&gt;
</pre>
</td>
</tr>
<tr><td><pre class="first last literal-block">
boost::is_same&lt;
    _
  , boost::add_pointer&lt;_&gt;
&gt;
</pre>
</td>
<td><pre class="first last literal-block">
boost::is_same&lt;
    _1
  , boost::add_pointer&lt;_1&gt;
&gt;
</pre>
</td>
</tr>
<tr><td><pre class="first last literal-block">
mpl::multiplies&lt;
   mpl::plus&lt;_,_&gt;
 , mpl::minus&lt;_,_&gt;
&gt;
</pre>
</td>
<td><pre class="first last literal-block">
mpl::multiplies&lt;
   mpl::plus&lt;_1,_2&gt;
 , mpl::minus&lt;_1,_2&gt;
&gt;
</pre>
</td>
</tr>
</tbody>
</table>
<!-- @ for n in range(len(stack)):
    stack[n].wrap('typedef ', 'type%d;' % n)
compile('all') -->
<p>Especially when used in simple lambda expressions, the unnamed
placeholder often eliminates just enough syntactic &quot;noise&quot; to
significantly improve readability.</p>
</div>
</div>

<div class="footer-separator"></div>
<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Prev</a>&nbsp;<a href="./placeholder-expression.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./placeholder-expression.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
</tr></table></body>
</html>