]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/flyweight/doc/tutorial/lambda_expressions.html
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / flyweight / doc / tutorial / lambda_expressions.html
CommitLineData
7c673cae
FG
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1 Transitional//EN">
2
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Boost.Flyweight Documentation - Tutorial - Annex - MPL lambda expressions</title>
7<link rel="stylesheet" href="../style.css" type="text/css">
8<link rel="start" href="../index.html">
9<link rel="prev" href="technical.html">
10<link rel="up" href="index.html">
11<link rel="next" href="../reference/index.html">
12</head>
13
14<body>
15<h1><img src="../../../../boost.png" alt="Boost logo" align=
16"middle" width="277" height="86">Boost.Flyweight Tutorial Annex: MPL lambda expressions</h1>
17
18<div class="prev_link"><a href="technical.html"><img src="../prev.gif" alt="technical issues" border="0"><br>
19Technical issues
20</a></div>
21<div class="up_link"><a href="index.html"><img src="../up.gif" alt="Boost.Flyweight tutorial" border="0"><br>
22Boost.Flyweight tutorial
23</a></div>
24<div class="next_link"><a href="../reference/index.html"><img src="../next.gif" alt="Boost.Flyweight reference" border="0"><br>
25Boost.Flyweight reference
26</a></div><br clear="all" style="clear: all;">
27
28<hr>
29
30<p>This short introduction to lambda expressions is meant for readers unfamiliar
31with the <a href="../../../mpl/doc/index.html">Boost MPL Library</a> who
32want to rapidly acquire a working knowledge of the basic concepts for the purposes
33of using them in Boost.Flyweight. Please refer to the Boost.MPL documentation
34for further information beyond these introductory notes.
35</p>
36
37<p>
38The specifiers defined by Boost.Flyweight rely heavily on the
39<a href="../../../mpl/doc/refmanual/lambda-expression.html"><code>Lambda
40Expression</code></a> concept defined by the
41<a href="../../../mpl/doc/index.html">Boost MPL Library</a>. A lambda
42expression can be thought of as a compile-time "type function", an entity (a
43concrete type, actually) that can be invoked with a list of types and returns
44some associated type in its turn. Consider for instance an arbitrary class
45template:
46</p>
47
48<blockquote><pre>
49<span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>T</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Q</span><span class=special>&gt;</span>
50<span class=keyword>class</span> <span class=identifier>foo</span>
51<span class=special>{</span>
52 <span class=special>...</span>
53<span class=special>};</span>
54</pre></blockquote>
55
56<p>
57and suppose we want to have a lambda expression that, when invoked
58with some generic types <code>Arg1</code> and <code>Arg2</code>,
59returns <code>foo&lt;Arg1,Arg2&gt;</code>. Such a lambda expression
60can be implemented in two ways
61<ol>
62 <li>As a
63 <a href="../../../mpl/doc/refmanual/metafunction-class.html"><code>MPL
64 Metafunction Class</code></a>, a type with a special nested class template
65 named <code>apply</code>:
66<blockquote><pre>
67<span class=keyword>struct</span> <span class=identifier>foo_specifier</span>
68<span class=special>{</span>
69 <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>Arg1</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Arg2</span><span class=special>&gt;</span>
70 <span class=keyword>struct</span> <span class=identifier>apply</span>
71 <span class=special>{</span>
72 <span class=comment>// this is the &quot;return type&quot; of foo_specifier</span>
73 <span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>Arg1</span><span class=special>,</span><span class=identifier>Arg2</span><span class=special>&gt;</span> <span class=identifier>type</span><span class=special>;</span>
74 <span class=special>};</span>
75<span class=special>};</span>
76</pre></blockquote>
77 </li>
78 <li>
79 As a
80 <a href="../../../mpl/doc/refmanual/placeholder-expression.html"><code>MPL
81 Placeholder Expression</code></a>, a class template instantiated with one or
82 more <i>placeholders</i>:
83<blockquote><pre>
84<span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_1</span><span class=special>,</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_2</span><span class=special>&gt;</span> <span class=identifier>foo_specifier</span><span class=special>;</span>
85</pre></blockquote>
86 Note that, in this case, <code>foo_specifier</code> is a concrete type, much
87 as <code>int</code> or <code>std::set&lt;std::string&gt;</code> are; yet,
88 MPL internal mechanisms are able to detect that this type has been gotten
89 from instantiating a class template with placeholders <code>boost::mpl::_1</code>
90 and <code>boost::mpl::_2</code> and take these placeholders as slots to
91 be substituted for actual types (the first and second type supplied,
92 respectively) when <code>foo_specifier</code> is
93 invoked. So, an instantiation of <code>foo</code> can be used
94 to refer back to the <code>foo</code> class template itself! The net
95 effect is the same as with metafunctions, but placeholder expressions spare
96 us the need to write boilerplate metafunction classes
97 --and the kind of metaprogramming magic they depend on has an undeniable
98 beauty to it.
99 </li>
100</ol>
101So far the examples shown just forward the arguments <code>Arg1</code> and
102<code>Arg2</code> directly to a class template without further elaboration,
103but there is nothing preventing us from doing some argument manipulation,
104like, for instance, switching their places:
105</p>
106
107<blockquote><pre>
108<span class=keyword>struct</span> <span class=identifier>foo_specifier</span>
109<span class=special>{</span>
110 <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>Arg1</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Arg2</span><span class=special>&gt;</span>
111 <span class=keyword>struct</span> <span class=identifier>apply</span><span class=special>{</span><span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>Arg2</span><span class=special>,</span><span class=identifier>Arg1</span><span class=special>&gt;</span> <span class=identifier>type</span><span class=special>;};</span>
112<span class=special>};</span>
113
114<span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_2</span><span class=special>,</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_1</span><span class=special>&gt;</span> <span class=identifier>foo_specifier</span><span class=special>;</span>
115</pre></blockquote>
116
117<p>
118passing placeholder subexpressions as arguments to the overall expression:
119</p>
120
121<blockquote><pre>
122<span class=keyword>struct</span> <span class=identifier>foo_specifier</span>
123<span class=special>{</span>
124 <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>Arg1</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Arg2</span><span class=special>&gt;</span>
125 <span class=keyword>struct</span> <span class=identifier>apply</span><span class=special>{</span><span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>shared_ptr</span><span class=special>&lt;</span><span class=identifier>Arg1</span><span class=special>&gt;,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>less</span><span class=special>&lt;</span><span class=identifier>Arg2</span><span class=special>&gt;</span> <span class=special>&gt;</span> <span class=identifier>type</span><span class=special>;};</span>
126<span class=special>};</span>
127
128<span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span>
129 <span class=identifier>boost</span><span class=special>::</span><span class=identifier>shared_ptr</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_1</span><span class=special>&gt;,</span>
130 <span class=identifier>std</span><span class=special>::</span><span class=identifier>less</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_2</span><span class=special>&gt;</span>
131<span class=special>&gt;</span> <span class=identifier>foo_specifier</span><span class=special>;</span>
132</pre></blockquote>
133
134<p>
135or accepting less or more arguments than the class template itself
136(the number of parameters of a lambda expression is called its <i>arity</i>):
137</p>
138
139<blockquote><pre>
140<span class=keyword>struct</span> <span class=identifier>foo_specifier</span>
141<span class=special>{</span>
142 <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>Arg1</span><span class=special>&gt;</span>
143 <span class=keyword>struct</span> <span class=identifier>apply</span><span class=special>{</span><span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>Arg1</span><span class=special>,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>less</span><span class=special>&lt;</span><span class=identifier>Arg1</span><span class=special>&gt;</span> <span class=identifier>type</span><span class=special>;};</span>
144<span class=special>};</span>
145
146<span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_1</span><span class=special>,</span><span class=identifier>std</span><span class=special>::</span><span class=identifier>less</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_1</span><span class=special>&gt;</span> <span class=special>&gt;</span> <span class=identifier>foo_specifier</span><span class=special>;</span>
147
148<span class=keyword>struct</span> <span class=identifier>foo_specifier</span>
149<span class=special>{</span>
150 <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>Arg1</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Arg2</span><span class=special>,</span><span class=keyword>typename</span> <span class=identifier>Arg3</span><span class=special>&gt;</span>
151 <span class=keyword>struct</span> <span class=identifier>apply</span><span class=special>{</span><span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>Arg1</span><span class=special>,</span><span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>Arg2</span><span class=special>,</span><span class=identifier>Arg3</span><span class=special>&gt;</span> <span class=special>&gt;</span> <span class=identifier>type</span><span class=special>;};</span>
152<span class=special>};</span>
153
154<span class=keyword>typedef</span> <span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_1</span><span class=special>,</span><span class=identifier>foo</span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_2</span><span class=special>,</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>mpl</span><span class=special>::</span><span class=identifier>_3</span><span class=special>&gt;</span> <span class=special>&gt;</span> <span class=identifier>foo_specifier</span><span class=special>;</span>
155</pre></blockquote>
156
157<hr>
158
159<div class="prev_link"><a href="technical.html"><img src="../prev.gif" alt="technical issues" border="0"><br>
160Technical issues
161</a></div>
162<div class="up_link"><a href="index.html"><img src="../up.gif" alt="Boost.Flyweight tutorial" border="0"><br>
163Boost.Flyweight tutorial
164</a></div>
165<div class="next_link"><a href="../reference/index.html"><img src="../next.gif" alt="Boost.Flyweight reference" border="0"><br>
166Boost.Flyweight reference
167</a></div><br clear="all" style="clear: all;">
168
169<br>
170
171<p>Revised August 13th 2008</p>
172
173<p>&copy; Copyright 2006-2008 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
174Distributed under the Boost Software
175License, Version 1.0. (See accompanying file <a href="../../../../LICENSE_1_0.txt">
176LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
177http://www.boost.org/LICENSE_1_0.txt</a>)
178</p>
179
180</body>
181</html>