Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/doc/html/lambda/s08.html @ 29

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

updated boost from 1_33_1 to 1_34_1

File size: 8.1 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.68.1">
7<link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8<link rel="up" href="../lambda.html" title="Chapter 8. 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 C++ Libraries" 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><h2 class="title" style="clear: both">
27<a name="id1249733"></a>Relation to other Boost libraries</h2></div></div></div>
28<div class="toc"><dl>
29<dt><span class="section"><a href="s08.html#id1249739">Boost Function</a></span></dt>
30<dt><span class="section"><a href="s08.html#id1249832">Boost Bind</a></span></dt>
31</dl></div>
32<div class="section" lang="en">
33<div class="titlepage"><div><div><h3 class="title">
34<a name="id1249739"></a>Boost Function</h3></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<p>
77
78</p>
79</div>
80<div class="section" lang="en">
81<div class="titlepage"><div><div><h3 class="title">
82<a name="id1249832"></a>Boost Bind</h3></div></div></div>
83<div class="toc"><dl><dt><span class="section"><a href="s08.html#id1249878">First argument of bind expression</a></span></dt></dl></div>
84<p>
85<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.
86Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL.
87There are, however, some semantical differerences.
88</p>
89<p>
90The BLL and BB evolved separately, and have different implementations.
91This means that the bind expressions from the BB cannot be used within
92bind expressions, or within other type of lambda expressions, of the BLL.
93The same holds for using BLL bind expressions in the BB.
94The libraries can coexist, however, as
95the names of the BB library are in <code class="literal">boost</code> namespace,
96whereas the BLL names are in <code class="literal">boost::lambda</code> namespace.
97</p>
98<p>
99The BLL requires a compiler that is reasonably conformant to the
100C++ standard, whereas the BB library is more portable, and works with
101a larger set of compilers.
102</p>
103<p>
104The following two sections describe what are the semantic differences
105between the bind expressions in BB and BLL.
106</p>
107<div class="section" lang="en">
108<div class="titlepage"><div><div><h4 class="title">
109<a name="id1249878"></a>First argument of bind expression</h4></div></div></div>
110
111In BB the first argument of the bind expression, the target function,
112is treated differently from the other arguments,
113as no argument substitution takes place within that argument.
114In BLL the first argument is not a special case in this respect.
115
116For example:
117
118<pre class="programlisting">
119template&lt;class F&gt;
120int foo(const F&amp; f) {
121  int x;
122  ..
123  bind(f, _1)(x);
124  ...
125}
126</pre>
127<pre class="programlisting">
128int bar(int, int);
129nested(bind(bar, 1, _1));
130</pre>
131
132The bind expression inside <code class="literal">foo</code> becomes:
133<pre class="programlisting">
134bind(bind(bar, 1, _1), _1)(x)
135</pre>
136
137The BLL interpretes this as:
138<pre class="programlisting">
139bar(1, x)(x)
140</pre>
141whereas the BB library as
142<pre class="programlisting">
143bar(1, x)
144</pre>
145
146To get this functionality in BLL, the bind expression inside the <code class="literal">foo</code> function can be written as:
147<pre class="programlisting">
148bind(unlambda(f), _1)(x);
149</pre>
150as explained in <a href="le_in_details.html#lambda.unlambda" title="Unlambda">the section called &#8220;Unlambda&#8221;</a>.
151
152</div>
153<p>
154The BB library supports up to nine placeholders, while the BLL
155defines only three placeholders.
156The rationale for not providing more, is that the highest arity of the
157function objects accepted by any STL algorithm is two.
158The placeholder count is easy to increase in the BB library.
159In BLL it is possible, but more laborous.
160The BLL currently passes the actual arguments to the lambda functors
161internally just as they are and does not wrap them inside a tuple object.
162The reason for this is that some widely used compilers are not capable
163of optimizing the intermediate tuple objects away.
164The creation of the intermediate tuples would cause a significant
165performance hit, particularly for the simplest (and thus the most common)
166lambda functors. 
167We are working on a hybrid approach, which will allow more placeholders
168but not compromise the performance of simple lambda functors.
169</p>
170</div>
171</div>
172<table width="100%"><tr>
173<td align="left"></td>
174<td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td>
175</tr></table>
176<hr>
177<div class="spirit-nav">
178<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>
179</div>
180</body>
181</html>
Note: See TracBrowser for help on using the repository browser.