• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Introduction</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../lambda.html" title="Chapter 20. Boost.Lambda">
10<link rel="prev" href="getting_started.html" title="Getting Started">
11<link rel="next" href="using_library.html" title="Using the library">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="getting_started.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="using_library.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="id-1.3.21.5"></a>Introduction</h2></div></div></div>
29<div class="toc"><dl class="toc">
30<dt><span class="section"><a href="s03.html#id-1.3.21.5.2">Motivation</a></span></dt>
31<dt><span class="section"><a href="s03.html#id-1.3.21.5.3">Introduction to lambda expressions</a></span></dt>
32</dl></div>
33<div class="section">
34<div class="titlepage"><div><div><h3 class="title">
35<a name="id-1.3.21.5.2"></a>Motivation</h3></div></div></div>
36<p>The Standard Template Library (STL)
37	<a class="xref" href="../lambda.html#cit:stepanov:94" title="The Standard Template Library">[<abbr class="abbrev">STL94</abbr>]</a>, now part of the C++ Standard Library <a class="xref" href="../lambda.html#cit:c++:98" title="International Standard, Programming Languages – C++">[<abbr class="abbrev">C++98</abbr>]</a>, is a generic container and algorithm library.
38Typically STL algorithms operate on container elements via <span class="emphasis"><em>function objects</em></span>. These function objects are passed as arguments to the algorithms.
39</p>
40<p>
41Any C++ construct that can be called with the function call syntax
42is a function object.
43The STL contains predefined function objects for some common cases (such as <code class="literal">plus</code>, <code class="literal">less</code> and <code class="literal">not1</code>).
44As an example, one possible implementation for the standard <code class="literal">plus</code> template is:
45
46</p>
47<pre class="programlisting">
48template &lt;class T&gt;
49struct plus : public binary_function&lt;T, T, T&gt; {
50  T operator()(const T&amp; i, const T&amp; j) const {
51    return i + j;
52  }
53};
54</pre>
55<p>
56
57The base class <code class="literal">binary_function&lt;T, T, T&gt;</code> contains typedefs for the argument and return types of the function object, which are needed to make the function object <span class="emphasis"><em>adaptable</em></span>.
58</p>
59<p>
60In addition to the basic function object classes, such as the one above,
61the STL contains <span class="emphasis"><em>binder</em></span> templates for creating a unary function object from an adaptable binary function object by fixing one of the arguments to a constant value.
62For example, instead of having to explicitly write a function object class like:
63
64</p>
65<pre class="programlisting">
66class plus_1 {
67  int _i;
68public:
69  plus_1(const int&amp; i) : _i(i) {}
70  int operator()(const int&amp; j) { return _i + j; }
71};
72</pre>
73<p>
74
75the equivalent functionality can be achieved with the <code class="literal">plus</code> template and one of the binder templates (<code class="literal">bind1st</code>).
76E.g., the following two expressions create function objects with identical functionalities;
77when invoked, both return the result of adding <code class="literal">1</code> to the argument of the function object:
78
79</p>
80<pre class="programlisting">
81plus_1(1)
82bind1st(plus&lt;int&gt;(), 1)
83</pre>
84<p>
85
86The subexpression <code class="literal">plus&lt;int&gt;()</code> in the latter line is a binary function object which computes the sum of two integers, and <code class="literal">bind1st</code> invokes this function object partially binding the first argument to <code class="literal">1</code>.
87As an example of using the above function object, the following code adds <code class="literal">1</code> to each element of some container <code class="literal">a</code> and outputs the results into the standard output stream <code class="literal">cout</code>.
88
89</p>
90<pre class="programlisting">
91transform(a.begin(), a.end(), ostream_iterator&lt;int&gt;(cout),
92          bind1st(plus&lt;int&gt;(), 1));
93</pre>
94<p>
95
96</p>
97<p>
98To make the binder templates more generally applicable, the STL contains <span class="emphasis"><em>adaptors</em></span> for making
99pointers or references to functions, and pointers to member functions,
100adaptable.
101
102Finally, some STL implementations contain function composition operations as
103extensions to the standard <a class="xref" href="../lambda.html#cit:sgi:02" title="The SGI Standard Template Library">[<abbr class="abbrev">SGI02</abbr>]</a>.
104      </p>
105<p>
106All these tools aim at one goal: to make it possible to specify
107<span class="emphasis"><em>unnamed functions</em></span> in a call of an STL algorithm,
108in other words, to pass code fragments as an argument to a function.
109
110However, this goal is attained only partially.
111The simple example above shows that the definition of unnamed functions
112with the standard tools is cumbersome.
113
114Complex expressions involving functors, adaptors, binders and
115function composition operations tend to be difficult to comprehend.
116
117In addition to this, there are significant restrictions in applying
118the standard tools. E.g. the standard binders allow only one argument
119of a binary function to be bound; there are no binders for
1203-ary, 4-ary etc. functions.
121</p>
122<p>
123The Boost Lambda Library provides solutions for the problems described above:
124
125</p>
126<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
127<li class="listitem">
128<p>
129Unnamed functions can be created easily with an intuitive syntax.
130
131The above example can be written as:
132
133</p>
134<pre class="programlisting">
135transform(a.begin(), a.end(), ostream_iterator&lt;int&gt;(cout),
136          1 + _1);
137</pre>
138<p>
139
140or even more intuitively:
141
142</p>
143<pre class="programlisting">
144for_each(a.begin(), a.end(), cout &lt;&lt; (1 + _1));
145</pre>
146<p>
147</p>
148</li>
149<li class="listitem"><p>
150Most of the restrictions in argument binding are removed,
151arbitrary arguments of practically any C++ function can be bound.
152</p></li>
153<li class="listitem"><p>
154Separate function composition operations are not needed,
155as function composition is supported implicitly.
156
157</p></li>
158</ul></div>
159<p>
160
161</p>
162</div>
163<div class="section">
164<div class="titlepage"><div><div><h3 class="title">
165<a name="id-1.3.21.5.3"></a>Introduction to lambda expressions</h3></div></div></div>
166<div class="toc"><dl class="toc">
167<dt><span class="section"><a href="s03.html#lambda.partial_function_application">Partial function application</a></span></dt>
168<dt><span class="section"><a href="s03.html#lambda.terminology">Terminology</a></span></dt>
169</dl></div>
170<p>
171	Lambda expression are common in functional programming languages.
172	Their syntax varies between languages (and between different forms of lambda calculus), but the basic form of a lambda expressions is:
173
174
175</p>
176<pre class="programlisting">
177lambda x<sub>1</sub> ... x<sub>n</sub>.e
178</pre>
179<p>
180
181
182	A lambda expression defines an unnamed function and consists of:
183	</p>
184<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
185<li class="listitem"><p>
186	      the parameters of this function: <code class="literal">x<sub>1</sub> ... x<sub>n</sub></code>.
187
188	    </p></li>
189<li class="listitem"><p>the expression e which computes the value of the function in terms of the parameters <code class="literal">x<sub>1</sub> ... x<sub>n</sub></code>.
190	    </p></li>
191</ul></div>
192<p>
193
194	A simple example of a lambda expression is
195</p>
196<pre class="programlisting">
197lambda x y.x+y
198</pre>
199<p>
200Applying the lambda function means substituting the formal parameters with the actual arguments:
201</p>
202<pre class="programlisting">
203(lambda x y.x+y) 2 3 = 2 + 3 = 5
204</pre>
205<p>
206
207
208      </p>
209<p>
210In the C++ version of lambda expressions the <code class="literal">lambda x<sub>1</sub> ... x<sub>n</sub></code> part is missing and the formal parameters have predefined names.
211In the current version of the library,
212there are three such predefined formal parameters,
213called <span class="emphasis"><em>placeholders</em></span>:
214<code class="literal">_1</code>, <code class="literal">_2</code> and <code class="literal">_3</code>.
215They refer to the first, second and third argument of the function defined
216by the lambda expression.
217
218For example, the C++ version of the definition
219</p>
220<pre class="programlisting">lambda x y.x+y</pre>
221<p>
222is
223</p>
224<pre class="programlisting">_1 + _2</pre>
225<p>
226</p>
227<p>
228Hence, there is no syntactic keyword for C++ lambda expressions.
229	The use of a placeholder as an operand implies that the operator invocation is a lambda expression.
230	However, this is true only for operator invocations.
231	Lambda expressions containing function calls, control structures, casts etc. require special syntactic constructs.
232	Most importantly, function calls need to be wrapped inside a <code class="literal">bind</code> function.
233
234	As an example, consider the lambda expression:
235
236	</p>
237<pre class="programlisting">lambda x y.foo(x,y)</pre>
238<p>
239
240	Rather than <code class="literal">foo(_1, _2)</code>, the C++ counterpart for this expression is:
241
242	</p>
243<pre class="programlisting">bind(foo, _1, _2)</pre>
244<p>
245
246	We refer to this type of C++ lambda expressions as <span class="emphasis"><em>bind expressions</em></span>.
247      </p>
248<p>A lambda expression defines a C++ function object, hence function application syntax is like calling any other function object, for instance: <code class="literal">(_1 + _2)(i, j)</code>.
249
250
251      </p>
252<div class="section">
253<div class="titlepage"><div><div><h4 class="title">
254<a name="lambda.partial_function_application"></a>Partial function application</h4></div></div></div>
255<p>
256A bind expression is in effect a <span class="emphasis"><em>partial function application</em></span>.
257In partial function application, some of the arguments of a function are bound to fixed values.
258	  The result is another function, with possibly fewer arguments.
259	  When called with the unbound arguments, this new function invokes the original function with the merged argument list of bound and unbound arguments.
260	</p>
261</div>
262<div class="section">
263<div class="titlepage"><div><div><h4 class="title">
264<a name="lambda.terminology"></a>Terminology</h4></div></div></div>
265<p>
266	  A lambda expression defines a function. A C++ lambda expression concretely constructs a function object, <span class="emphasis"><em>a functor</em></span>, when evaluated. We use the name <span class="emphasis"><em>lambda functor</em></span> to refer to such a function object.
267	  Hence, in the terminology adopted here, the result of evaluating a lambda expression is a lambda functor.
268	</p>
269</div>
270</div>
271</div>
272<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
273<td align="left"></td>
274<td align="right"><div class="copyright-footer">Copyright © 1999-2004 Jaakko Järvi, Gary Powell<p>Use, modification and distribution is subject to the Boost
275    Software License, Version 1.0. (See accompanying file
276    <code class="filename">LICENSE_1_0.txt</code> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</p>
277</div></td>
278</tr></table>
279<hr>
280<div class="spirit-nav">
281<a accesskey="p" href="getting_started.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="using_library.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
282</div>
283</body>
284</html>
285