]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/mpl/doc/tutorial/placeholders.html
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / mpl / doc / tutorial / placeholders.html
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>&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>
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 &quot;placeholder&quot; 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&lt;X&gt;</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
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>
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
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
36 provided.</td></tr>
37 </tbody>
38 </table>
39 <pre class="literal-block">
40 namespace boost { namespace mpl { namespace placeholders {
41
42 template &lt;int N&gt; struct arg; // forward declarations
43 struct void_;
44
45 template &lt;&gt;
46 struct arg&lt;<strong>1</strong>&gt;
47 {
48 template &lt;
49 class <strong>A1</strong>, class A2 = void_, ... class A<em>m</em> = void_&gt;
50 struct apply
51 {
52 typedef <strong>A1</strong> type; // return the first argument
53 };
54 };
55 typedef <strong>arg&lt;1&gt; _1</strong>;
56
57 template &lt;&gt;
58 struct arg&lt;<strong>2</strong>&gt;
59 {
60 template &lt;
61 class A1, class <strong>A2</strong>, class A3 = void_, ...class A<em>m</em> = void_
62 &gt;
63 struct apply
64 {
65 typedef <strong>A2</strong> type; // return the second argument
66 };
67 };
68 typedef <strong>arg&lt;2&gt; _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
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>
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
85 placeholder</strong>, that we haven't yet defined:</p>
86 <pre class="literal-block">
87 namespace boost { namespace mpl { namespace placeholders {
88
89 <strong>typedef arg&lt;-1&gt; _;</strong> // the unnamed placeholder
90
91 }}}
92 </pre>
93 <!-- @ stack[-2].prepend('namespace shield {')
94 example.append('}') # so we don't conflict with the prefix
95 compile('all') -->
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>
100 <blockquote>
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>
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">
113 mpl::plus&lt;_,_&gt;
114 </pre>
115 </td>
116 <td><pre class="first last literal-block">
117 mpl::plus&lt;_1,_2&gt;
118 </pre>
119 </td>
120 </tr>
121 <tr><td><pre class="first last literal-block">
122 boost::is_same&lt;
123 _
124 , boost::add_pointer&lt;_&gt;
125 &gt;
126 </pre>
127 </td>
128 <td><pre class="first last literal-block">
129 boost::is_same&lt;
130 _1
131 , boost::add_pointer&lt;_1&gt;
132 &gt;
133 </pre>
134 </td>
135 </tr>
136 <tr><td><pre class="first last literal-block">
137 mpl::multiplies&lt;
138 mpl::plus&lt;_,_&gt;
139 , mpl::minus&lt;_,_&gt;
140 &gt;
141 </pre>
142 </td>
143 <td><pre class="first last literal-block">
144 mpl::multiplies&lt;
145 mpl::plus&lt;_1,_2&gt;
146 , mpl::minus&lt;_1,_2&gt;
147 &gt;
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)
155 compile('all') -->
156 <p>Especially when used in simple lambda expressions, the unnamed
157 placeholder often eliminates just enough syntactic &quot;noise&quot; to
158 significantly 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>&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>
164 </tr></table></body>
165 </html>