1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
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">
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>
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>
27 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
28 <a name="idp362475616"></a>Introduction</h2></div></div></div>
29 <div class="toc"><dl class="toc">
30 <dt><span class="section"><a href="s03.html#idp362476288">Motivation</a></span></dt>
31 <dt><span class="section"><a href="s03.html#idp362505120">Introduction to lambda expressions</a></span></dt>
34 <div class="titlepage"><div><div><h3 class="title">
35 <a name="idp362476288"></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.
38 Typically 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.
41 Any C++ construct that can be called with the function call syntax
43 The 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>).
44 As an example, one possible implementation for the standard <code class="literal">plus</code> template is:
47 <pre class="programlisting">
48 template <class T>
49 struct plus : public binary_function<T, T, T> {
50 T operator()(const T& i, const T& j) const {
57 The base class <code class="literal">binary_function<T, T, T></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>.
60 In addition to the basic function object classes, such as the one above,
61 the 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.
62 For example, instead of having to explicitly write a function object class like:
65 <pre class="programlisting">
69 plus_1(const int& i) : _i(i) {}
70 int operator()(const int& j) { return _i + j; }
75 the equivalent functionality can be achieved with the <code class="literal">plus</code> template and one of the binder templates (<code class="literal">bind1st</code>).
76 E.g., the following two expressions create function objects with identical functionalities;
77 when invoked, both return the result of adding <code class="literal">1</code> to the argument of the function object:
80 <pre class="programlisting">
82 bind1st(plus<int>(), 1)
86 The subexpression <code class="literal">plus<int>()</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>.
87 As 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>.
90 <pre class="programlisting">
91 transform(a.begin(), a.end(), ostream_iterator<int>(cout),
92 bind1st(plus<int>(), 1));
98 To make the binder templates more generally applicable, the STL contains <span class="emphasis"><em>adaptors</em></span> for making
99 pointers or references to functions, and pointers to member functions,
102 Finally, some STL implementations contain function composition operations as
103 extensions to the standard <a class="xref" href="../lambda.html#cit:sgi:02" title="The SGI Standard Template Library">[<abbr class="abbrev">SGI02</abbr>]</a>.
106 All 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,
108 in other words, to pass code fragments as an argument to a function.
110 However, this goal is attained only partially.
111 The simple example above shows that the definition of unnamed functions
112 with the standard tools is cumbersome.
114 Complex expressions involving functors, adaptors, binders and
115 function composition operations tend to be difficult to comprehend.
117 In addition to this, there are significant restrictions in applying
118 the standard tools. E.g. the standard binders allow only one argument
119 of a binary function to be bound; there are no binders for
120 3-ary, 4-ary etc. functions.
123 The Boost Lambda Library provides solutions for the problems described above:
126 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
127 <li class="listitem">
129 Unnamed functions can be created easily with an intuitive syntax.
131 The above example can be written as:
134 <pre class="programlisting">
135 transform(a.begin(), a.end(), ostream_iterator<int>(cout),
140 or even more intuitively:
143 <pre class="programlisting">
144 for_each(a.begin(), a.end(), cout << (1 + _1));
149 <li class="listitem"><p>
150 Most of the restrictions in argument binding are removed,
151 arbitrary arguments of practically any C++ function can be bound.
153 <li class="listitem"><p>
154 Separate function composition operations are not needed,
155 as function composition is supported implicitly.
163 <div class="section">
164 <div class="titlepage"><div><div><h3 class="title">
165 <a name="idp362505120"></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>
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:
176 <pre class="programlisting">
177 lambda x<sub>1</sub> ... x<sub>n</sub>.e
182 A lambda expression defines an unnamed function and consists of:
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>.
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>.
194 A simple example of a lambda expression is
196 <pre class="programlisting">
200 Applying the lambda function means substituting the formal parameters with the actual arguments:
202 <pre class="programlisting">
203 (lambda x y.x+y) 2 3 = 2 + 3 = 5
210 In 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.
211 In the current version of the library,
212 there are three such predefined formal parameters,
213 called <span class="emphasis"><em>placeholders</em></span>:
214 <code class="literal">_1</code>, <code class="literal">_2</code> and <code class="literal">_3</code>.
215 They refer to the first, second and third argument of the function defined
216 by the lambda expression.
218 For example, the C++ version of the definition
220 <pre class="programlisting">lambda x y.x+y</pre>
224 <pre class="programlisting">_1 + _2</pre>
228 Hence, 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.
234 As an example, consider the lambda expression:
237 <pre class="programlisting">lambda x y.foo(x,y)</pre>
240 Rather than <code class="literal">foo(_1, _2)</code>, the C++ counterpart for this expression is:
243 <pre class="programlisting">bind(foo, _1, _2)</pre>
246 We refer to this type of C++ lambda expressions as <span class="emphasis"><em>bind expressions</em></span>.
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>.
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>
256 A bind expression is in effect a <span class="emphasis"><em>partial function application</em></span>.
257 In 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.
262 <div class="section">
263 <div class="titlepage"><div><div><h4 class="title">
264 <a name="lambda.terminology"></a>Terminology</h4></div></div></div>
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.
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>
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>