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) --> 7<head> 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" /> 12</head> 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> 23</div> 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 28select the <em>N</em>th argument for any <em>N</em>. <a class="footnote-reference" href="#config" id="id12" name="id12">[6]</a> The 29implementation of placeholders looks something like this:</p> 30<table class="footnote" frame="void" id="config" rules="none"> 31<colgroup><col class="label" /><col /></colgroup> 32<tbody valign="top"> 33<tr><td class="label"><a class="fn-backref" href="#id12" name="config">[6]</a></td><td>MPL provides five placeholders by default. See 34the Configuration Macros section of <a class="reference" href="./reference-manual.html">the MPL reference manual</a> for a 35description of how to change the number of placeholders 36provided.</td></tr> 37</tbody> 38</table> 39<pre class="literal-block"> 40namespace boost { namespace mpl { namespace placeholders { 41 42template <int N> struct arg; // forward declarations 43struct void_; 44 45template <> 46struct arg<<strong>1</strong>> 47{ 48 template < 49 class <strong>A1</strong>, class A2 = void_, ... class A<em>m</em> = void_> 50 struct apply 51 { 52 typedef <strong>A1</strong> type; // return the first argument 53 }; 54}; 55typedef <strong>arg<1> _1</strong>; 56 57template <> 58struct arg<<strong>2</strong>> 59{ 60 template < 61 class A1, class <strong>A2</strong>, class A3 = void_, ...class A<em>m</em> = void_ 62 > 63 struct apply 64 { 65 typedef <strong>A2</strong> type; // return the second argument 66 }; 67}; 68typedef <strong>arg<2> _2</strong>; 69 70<em>more specializations and typedefs...</em> 71 72}}} 73</pre> 74<!-- @example.replace('...','') --> 75<p>Remember that invoking a metafunction class is the same as invoking 76its nested <tt class="literal"><span class="pre">apply</span></tt> metafunction. When a placeholder in a lambda 77expression is evaluated, it is invoked on the expression's actual 78arguments, returning just one of them. The results are then 79substituted back into the lambda expression and the evaluation 80process continues.</p> 81</div> 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 85placeholder</strong>, that we haven't yet defined:</p> 86<pre class="literal-block"> 87namespace boost { namespace mpl { namespace placeholders { 88 89<strong>typedef arg<-1> _;</strong> // the unnamed placeholder 90 91}}} 92</pre> 93<!-- @ stack[-2].prepend('namespace shield {') 94example.append('}') # so we don't conflict with the prefix 95compile('all') --> 96<p>The details of its implementation aren't important; all you really 97need to know about the unnamed placeholder is that it gets special 98treatment. When a lambda expression is being transformed into a 99metafunction class by <tt class="literal"><span class="pre">mpl::lambda</span></tt>,</p> 100<blockquote> 101the <em>n</em>th appearance of the unnamed placeholder <em>in a given 102template 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 104below contains two equivalent lambda expressions.</p> 105<table border="1" class="table"> 106<caption>Unnamed Placeholder Semantics</caption> 107<colgroup> 108<col width="48%" /> 109<col width="52%" /> 110</colgroup> 111<tbody valign="top"> 112<tr><td><pre class="first last literal-block"> 113mpl::plus<_,_> 114</pre> 115</td> 116<td><pre class="first last literal-block"> 117mpl::plus<_1,_2> 118</pre> 119</td> 120</tr> 121<tr><td><pre class="first last literal-block"> 122boost::is_same< 123 _ 124 , boost::add_pointer<_> 125> 126</pre> 127</td> 128<td><pre class="first last literal-block"> 129boost::is_same< 130 _1 131 , boost::add_pointer<_1> 132> 133</pre> 134</td> 135</tr> 136<tr><td><pre class="first last literal-block"> 137mpl::multiplies< 138 mpl::plus<_,_> 139 , mpl::minus<_,_> 140> 141</pre> 142</td> 143<td><pre class="first last literal-block"> 144mpl::multiplies< 145 mpl::plus<_1,_2> 146 , mpl::minus<_1,_2> 147> 148</pre> 149</td> 150</tr> 151</tbody> 152</table> 153<!-- @ for n in range(len(stack)): 154 stack[n].wrap('typedef ', 'type%d;' % n) 155compile('all') --> 156<p>Especially when used in simple lambda expressions, the unnamed 157placeholder often eliminates just enough syntactic "noise" to 158significantly improve readability.</p> 159</div> 160</div> 161 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> 164</tr></table></body> 165</html> 166