Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/lambda/s08.html @ 25

Last change on this file since 25 was 12, checked in by landauf, 17 years ago

added boost

File size: 8.0 KB
Line 
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4<title>Relation to other Boost libraries</title>
5<link rel="stylesheet" href="../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
7<link rel="start" href="../index.html" title="The Boost C++ Libraries">
8<link rel="up" href="../lambda.html" title="Chapter 6. Boost.Lambda">
9<link rel="prev" href="s07.html" title="Practical considerations">
10<link rel="next" href="s09.html" title="Contributors">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%">
14<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td>
15<td align="center"><a href="../../../index.htm">Home</a></td>
16<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="../../../people/people.htm">People</a></td>
18<td align="center"><a href="../../../more/faq.htm">FAQ</a></td>
19<td align="center"><a href="../../../more/index.htm">More</a></td>
20</table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="s07.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="s09.html"><img src="../images/next.png" alt="Next"></a>
24</div>
25<div class="section" lang="en">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="id2713384"></a>Relation to other Boost libraries</h3></div></div></div>
28<div class="toc"><dl>
29<dt><span class="section"><a href="s08.html#id2713388">Boost Function</a></span></dt>
30<dt><span class="section"><a href="s08.html#id2713474">Boost Bind</a></span></dt>
31</dl></div>
32<div class="section" lang="en">
33<div class="titlepage"><div><div><h4 class="title">
34<a name="id2713388"></a>Boost Function</h4></div></div></div>
35<p>Sometimes it is convenient to store lambda functors in variables.
36However, 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 href="../lambda.html#cit:boost::function" title="[function]"><span class="abbrev">function</span></a>] defines wrappers for arbitrary function objects, for example
38lambda functors; and these wrappers have types that are easy to type out.
39
40For example:
41
42</p>
43<pre class="programlisting">
44boost::function&lt;int(int, int)&gt; f = _1 + _2;
45boost::function&lt;int&amp;(int&amp;)&gt; g = (_1 += 10);
46int i = 1, j = 2;
47f(i, j); // returns 3
48g(i);    // sets i to = 11;
49</pre>
50<p>
51
52The 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.
53Wrapping 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.
54
55Note that storing lambda functors inside <code class="literal">boost::function</code> 
56introduces a danger.
57Certain types of lambda functors may store references to the bound
58arguments, instead as taking copies of the arguments of the lambda expression.
59When temporary lambda functor objects are used
60in STL algorithm invocations this is always safe, as the lambda functor gets
61destructed immediately after the STL algortihm invocation is completed.
62
63However, a lambda functor wrapped inside <code class="literal">boost::function</code> 
64may continue to exist longer, creating the possibility of dangling references.
65For example:
66
67</p>
68<pre class="programlisting">
69int* sum = new int();
70*sum = 0;
71boost::function&lt;int&amp;(int)&gt; counter = *sum += _1;
72counter(5); // ok, *sum = 5;
73delete sum;
74counter(3); // error, *sum does not exist anymore
75</pre>
76</div>
77<div class="section" lang="en">
78<div class="titlepage"><div><div><h4 class="title">
79<a name="id2713474"></a>Boost Bind</h4></div></div></div>
80<div class="toc"><dl><dt><span class="section"><a href="s08.html#id2713521">First argument of bind expression</a></span></dt></dl></div>
81<p><span class="emphasis"><em>The Boost Bind</em></span>[<a href="../lambda.html#cit:boost::bind" title="[bind]"><span class="abbrev">bind</span></a>] library has partially overlapping functionality with the BLL.
82Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL.
83There are, however, some semantical differerences.
84</p>
85<p>
86The BLL and BB evolved separately, and have different implementations.
87This means that the bind expressions from the BB cannot be used within
88bind expressions, or within other type of lambda expressions, of the BLL.
89The same holds for using BLL bind expressions in the BB.
90The libraries can coexist, however, as
91the names of the BB library are in <code class="literal">boost</code> namespace,
92whereas the BLL names are in <code class="literal">boost::lambda</code> namespace.
93</p>
94<p>
95The BLL requires a compiler that is reasonably conformant to the
96C++ standard, whereas the BB library is more portable, and works with
97a larger set of compilers.
98</p>
99<p>
100The following two sections describe what are the semantic differences
101between the bind expressions in BB and BLL.
102</p>
103<div class="section" lang="en">
104<div class="titlepage"><div><div><h5 class="title">
105<a name="id2713521"></a>First argument of bind expression</h5></div></div></div>
106
107In BB the first argument of the bind expression, the target function,
108is treated differently from the other arguments,
109as no argument substitution takes place within that argument.
110In BLL the first argument is not a special case in this respect.
111
112For example:
113
114<pre class="programlisting">
115template&lt;class F&gt;
116int foo(const F&amp; f) {
117  int x;
118  ..
119  bind(f, _1)(x);
120  ...
121}
122</pre>
123<pre class="programlisting">
124int bar(int, int);
125nested(bind(bar, 1, _1));
126</pre>
127
128The bind expression inside <code class="literal">foo</code> becomes:
129<pre class="programlisting">
130bind(bind(bar, 1, _1), _1)(x)
131</pre>
132
133The BLL interpretes this as:
134<pre class="programlisting">
135bar(1, x)(x)
136</pre>
137whereas the BB library as
138<pre class="programlisting">
139bar(1, x)
140</pre>
141
142To get this functionality in BLL, the bind expression inside the <code class="literal">foo</code> function can be written as:
143<pre class="programlisting">
144bind(unlambda(f), _1)(x);
145</pre>
146as explained in <a href="le_in_details.html#lambda.unlambda" title="Unlambda">the section called &#8220;Unlambda&#8221;</a>.
147
148</div>
149<p>
150The BB library supports up to nine placeholders, while the BLL
151defines only three placeholders.
152The rationale for not providing more, is that the highest arity of the
153function objects accepted by any STL algorithm is two.
154The placeholder count is easy to increase in the BB library.
155In BLL it is possible, but more laborous.
156The BLL currently passes the actual arguments to the lambda functors
157internally just as they are and does not wrap them inside a tuple object.
158The reason for this is that some widely used compilers are not capable
159of optimizing the intermediate tuple objects away.
160The creation of the intermediate tuples would cause a significant
161performance hit, particularly for the simplest (and thus the most common)
162lambda functors. 
163We are working on a hybrid approach, which will allow more placeholders
164but not compromise the performance of simple lambda functors.
165</p>
166</div>
167</div>
168<table width="100%"><tr>
169<td align="left"></td>
170<td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td>
171</tr></table>
172<hr>
173<div class="spirit-nav">
174<a accesskey="p" href="s07.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="s09.html"><img src="../images/next.png" alt="Next"></a>
175</div>
176</body>
177</html>
Note: See TracBrowser for help on using the repository browser.