1 <?xml version=
"1.0" encoding=
"utf-8" ?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
4 <!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
5 <!-- Software License, Version 1.0. (See accompanying -->
6 <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
8 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" />
9 <meta name=
"generator" content=
"Docutils 0.3.6: http://docutils.sourceforge.net/" />
10 <title>THE BOOST MPL LIBRARY: Placeholders
</title>
11 <link rel=
"stylesheet" href=
"../style.css" type=
"text/css" />
13 <body class=
"docframe">
14 <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> <a href=
"./placeholder-expression.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group">Back
<a href=
"./placeholder-expression.html" class=
"navigation-link">Along
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./lambda-details.html" class=
"navigation-link">Up
</a> <a href=
"../index.html" class=
"navigation-link">Home
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./tutorial_toc.html" class=
"navigation-link">Full TOC
</a></span></td>
15 <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>
16 </tr></table><div class=
"header-separator"></div>
17 <div class=
"section" id=
"placeholders">
18 <h1><a class=
"toc-backref" href=
"./lambda-details.html#id55" name=
"placeholders">Placeholders
</a></h1>
19 <p>The definition of
"placeholder
" may surprise you:
</p>
20 <div class=
"admonition-definition admonition">
21 <p class=
"admonition-title first">Definition
</p>
22 <p>A
<strong>placeholder
</strong> is a metafunction class of the form
<tt class=
"literal"><span class=
"pre">mpl::arg
<X
></span></tt>.
</p>
24 <div class=
"section" id=
"implementation">
25 <h2><a name=
"implementation">Implementation
</a></h2>
26 <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
27 <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
28 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
29 implementation of placeholders looks something like this:
</p>
30 <table class=
"footnote" frame=
"void" id=
"config" rules=
"none">
31 <colgroup><col class=
"label" /><col /></colgroup>
33 <tr><td class=
"label"><a class=
"fn-backref" href=
"#id12" name=
"config">[
6]
</a></td><td>MPL provides five placeholders by default. See
34 the Configuration Macros section of
<a class=
"reference" href=
"./reference-manual.html">the MPL reference manual
</a> for a
35 description of how to change the number of placeholders
39 <pre class=
"literal-block">
40 namespace boost { namespace mpl { namespace placeholders {
42 template
<int N
> struct arg; // forward declarations
46 struct arg
<<strong>1</strong>>
49 class
<strong>A1
</strong>, class A2 = void_, ... class A
<em>m
</em> = void_
>
52 typedef
<strong>A1
</strong> type; // return the first argument
55 typedef
<strong>arg
<1> _1
</strong>;
58 struct arg
<<strong>2</strong>>
61 class A1, class
<strong>A2
</strong>, class A3 = void_, ...class A
<em>m
</em> = void_
65 typedef
<strong>A2
</strong> type; // return the second argument
68 typedef
<strong>arg
<2> _2
</strong>;
70 <em>more specializations and typedefs...
</em>
74 <!-- @example.replace('...','') -->
75 <p>Remember that invoking a metafunction class is the same as invoking
76 its nested
<tt class=
"literal"><span class=
"pre">apply
</span></tt> metafunction. When a placeholder in a lambda
77 expression is evaluated, it is invoked on the expression's actual
78 arguments, returning just one of them. The results are then
79 substituted back into the lambda expression and the evaluation
80 process continues.
</p>
82 <div class=
"section" id=
"the-unnamed-placeholder">
83 <h2><a name=
"the-unnamed-placeholder">The Unnamed Placeholder
</a></h2>
84 <p>There's one special placeholder, known as the
<strong>unnamed
85 placeholder
</strong>, that we haven't yet defined:
</p>
86 <pre class=
"literal-block">
87 namespace boost { namespace mpl { namespace placeholders {
89 <strong>typedef arg
<-
1> _;
</strong> // the unnamed placeholder
93 <!-- @ stack[-2].prepend('namespace shield {')
94 example.append('}') # so we don't conflict with the prefix
96 <p>The details of its implementation aren't important; all you really
97 need to know about the unnamed placeholder is that it gets special
98 treatment. When a lambda expression is being transformed into a
99 metafunction class by
<tt class=
"literal"><span class=
"pre">mpl::lambda
</span></tt>,
</p>
101 the
<em>n
</em>th appearance of the unnamed placeholder
<em>in a given
102 template specialization
</em> is replaced with
<tt class=
"literal"><span class=
"pre">_
</span></tt><em>n
</em>.
</blockquote>
103 <p>So, for example, every row of Table
1.1
104 below contains two equivalent lambda expressions.
</p>
105 <table border=
"1" class=
"table">
106 <caption>Unnamed Placeholder Semantics
</caption>
112 <tr><td><pre class=
"first last literal-block">
116 <td><pre class=
"first last literal-block">
117 mpl::plus
<_1,_2
>
121 <tr><td><pre class=
"first last literal-block">
124 , boost::add_pointer
<_
>
128 <td><pre class=
"first last literal-block">
131 , boost::add_pointer
<_1
>
136 <tr><td><pre class=
"first last literal-block">
139 , mpl::minus
<_,_
>
143 <td><pre class=
"first last literal-block">
145 mpl::plus
<_1,_2
>
146 , mpl::minus
<_1,_2
>
153 <!-- @ for n in range(len(stack)):
154 stack[n].wrap('typedef ', 'type%d;' % n)
156 <p>Especially when used in simple lambda expressions, the unnamed
157 placeholder often eliminates just enough syntactic
"noise
" to
158 significantly improve readability.
</p>
162 <div class=
"footer-separator"></div>
163 <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> <a href=
"./placeholder-expression.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group">Back
<a href=
"./placeholder-expression.html" class=
"navigation-link">Along
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./lambda-details.html" class=
"navigation-link">Up
</a> <a href=
"../index.html" class=
"navigation-link">Home
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./tutorial_toc.html" class=
"navigation-link">Full TOC
</a></span></td>