3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Relation to other Boost libraries</title>
5 <link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8 <link rel="up" href="../lambda.html" title="Chapter 14. Boost.Lambda">
9 <link rel="prev" href="s07.html" title="Practical considerations">
10 <link rel="next" href="s09.html" title="Contributors">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
15 <td align="center"><a href="../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="s07.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="s09.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="id2420502"></a>Relation to other Boost libraries</h2></div></div></div>
29 <dt><span class="section"><a href="s08.html#id2420509">Boost Function</a></span></dt>
30 <dt><span class="section"><a href="s08.html#id2420601">Boost Bind</a></span></dt>
33 <div class="titlepage"><div><div><h3 class="title">
34 <a name="id2420509"></a>Boost Function</h3></div></div></div>
35 <p>Sometimes it is convenient to store lambda functors in variables.
36 However, the types of even the simplest lambda functors are long and unwieldy, and it is in general unfeasible to declare variables with lambda functor types.
37 <span class="emphasis"><em>The Boost Function library</em></span> <a class="xref" href="../lambda.html#cit:boost::function" title="Boost Function Library">[<abbr class="abbrev">function</abbr>]</a> defines wrappers for arbitrary function objects, for example
38 lambda functors; and these wrappers have types that are easy to type out.
43 <pre class="programlisting">
44 boost::function<int(int, int)> f = _1 + _2;
45 boost::function<int&(int&)> g = (_1 += 10);
48 g(i); // sets i to = 11;
52 The return and parameter types of the wrapped function object must be written explicilty as the template argument to the wrapper template <code class="literal">boost::function</code>; even when lambda functors, which otherwise have generic parameters, are wrapped.
53 Wrapping a function object with <code class="literal">boost::function</code> introduces a performance cost comparable to virtual function dispatch, though virtual functions are not actually used.
55 Note that storing lambda functors inside <code class="literal">boost::function</code>
57 Certain types of lambda functors may store references to the bound
58 arguments, instead as taking copies of the arguments of the lambda expression.
59 When temporary lambda functor objects are used
60 in STL algorithm invocations this is always safe, as the lambda functor gets
61 destructed immediately after the STL algortihm invocation is completed.
63 However, a lambda functor wrapped inside <code class="literal">boost::function</code>
64 may continue to exist longer, creating the possibility of dangling references.
68 <pre class="programlisting">
71 boost::function<int&(int)> counter = *sum += _1;
72 counter(5); // ok, *sum = 5;
74 counter(3); // error, *sum does not exist anymore
81 <div class="titlepage"><div><div><h3 class="title">
82 <a name="id2420601"></a>Boost Bind</h3></div></div></div>
83 <div class="toc"><dl><dt><span class="section"><a href="s08.html#id2420653">First argument of bind expression</a></span></dt></dl></div>
85 <span class="emphasis"><em>The Boost Bind</em></span> <a class="xref" href="../lambda.html#cit:boost::bind" title="Boost Bind Library">[<abbr class="abbrev">bind</abbr>]</a> library has partially overlapping functionality with the BLL.
86 Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL.
87 There are, however, some semantical differerences.
90 The BLL and BB evolved separately, and have different implementations.
91 This means that the bind expressions from the BB cannot be used within
92 bind expressions, or within other type of lambda expressions, of the BLL.
93 The same holds for using BLL bind expressions in the BB.
94 The libraries can coexist, however, as
95 the names of the BB library are in <code class="literal">boost</code> namespace,
96 whereas the BLL names are in <code class="literal">boost::lambda</code> namespace.
99 The BLL requires a compiler that is reasonably conformant to the
100 C++ standard, whereas the BB library is more portable, and works with
101 a larger set of compilers.
104 The following two sections describe what are the semantic differences
105 between the bind expressions in BB and BLL.
107 <div class="section">
108 <div class="titlepage"><div><div><h4 class="title">
109 <a name="id2420653"></a>First argument of bind expression</h4></div></div></div>
111 In BB the first argument of the bind expression, the target function,
112 is treated differently from the other arguments,
113 as no argument substitution takes place within that argument.
114 In BLL the first argument is not a special case in this respect.
118 <pre class="programlisting">
119 template<class F>
120 int foo(const F& f) {
127 <pre class="programlisting">
129 nested(bind(bar, 1, _1));
132 The bind expression inside <code class="literal">foo</code> becomes:
133 <pre class="programlisting">
134 bind(bind(bar, 1, _1), _1)(x)
137 The BLL interpretes this as:
138 <pre class="programlisting">
141 whereas the BB library as
142 <pre class="programlisting">
146 To get this functionality in BLL, the bind expression inside the <code class="literal">foo</code> function can be written as:
147 <pre class="programlisting">
148 bind(unlambda(f), _1)(x);
150 as explained in <a class="xref" href="le_in_details.html#lambda.unlambda" title="Unlambda">the section called “Unlambda”</a>.
154 The BB library supports up to nine placeholders, while the BLL
155 defines only three placeholders.
156 The rationale for not providing more, is that the highest arity of the
157 function objects accepted by any STL algorithm is two.
158 The placeholder count is easy to increase in the BB library.
159 In BLL it is possible, but more laborous.
160 The BLL currently passes the actual arguments to the lambda functors
161 internally just as they are and does not wrap them inside a tuple object.
162 The reason for this is that some widely used compilers are not capable
163 of optimizing the intermediate tuple objects away.
164 The creation of the intermediate tuples would cause a significant
165 performance hit, particularly for the simplest (and thus the most common)
167 We are working on a hybrid approach, which will allow more placeholders
168 but not compromise the performance of simple lambda functors.
172 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
173 <td align="left"></td>
174 <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
175 Software License, Version 1.0. (See accompanying file
176 <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>
180 <div class="spirit-nav">
181 <a accesskey="p" href="s07.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="s09.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>