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>Relation to other Boost libraries</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="s07.html" title="Practical considerations"> 11<link rel="next" href="s09.html" title="Contributors"> 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="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> 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.10"></a>Relation to other Boost libraries</h2></div></div></div> 29<div class="toc"><dl class="toc"> 30<dt><span class="section"><a href="s08.html#id-1.3.21.10.2">Boost Function</a></span></dt> 31<dt><span class="section"><a href="s08.html#id-1.3.21.10.3">Boost Bind</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.10.2"></a>Boost Function</h3></div></div></div> 36<p>Sometimes it is convenient to store lambda functors in variables. 37However, 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. 38<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 39lambda functors; and these wrappers have types that are easy to type out. 40 41For example: 42 43</p> 44<pre class="programlisting"> 45boost::function<int(int, int)> f = _1 + _2; 46boost::function<int&(int&)> g = (_1 += 10); 47int i = 1, j = 2; 48f(i, j); // returns 3 49g(i); // sets i to = 11; 50</pre> 51<p> 52 53The 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. 54Wrapping 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 56Note that storing lambda functors inside <code class="literal">boost::function</code> 57introduces a danger. 58Certain types of lambda functors may store references to the bound 59arguments, instead as taking copies of the arguments of the lambda expression. 60When temporary lambda functor objects are used 61in STL algorithm invocations this is always safe, as the lambda functor gets 62destructed immediately after the STL algortihm invocation is completed. 63 64However, a lambda functor wrapped inside <code class="literal">boost::function</code> 65may continue to exist longer, creating the possibility of dangling references. 66For example: 67 68</p> 69<pre class="programlisting"> 70int* sum = new int(); 71*sum = 0; 72boost::function<int&(int)> counter = *sum += _1; 73counter(5); // ok, *sum = 5; 74delete sum; 75counter(3); // error, *sum does not exist anymore 76</pre> 77<p> 78 79</p> 80</div> 81<div class="section"> 82<div class="titlepage"><div><div><h3 class="title"> 83<a name="id-1.3.21.10.3"></a>Boost Bind</h3></div></div></div> 84<div class="toc"><dl class="toc"><dt><span class="section"><a href="s08.html#id-1.3.21.10.3.6">First argument of bind expression</a></span></dt></dl></div> 85<p> 86<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. 87Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL. 88There are, however, some semantical differerences. 89</p> 90<p> 91The BLL and BB evolved separately, and have different implementations. 92This means that the bind expressions from the BB cannot be used within 93bind expressions, or within other type of lambda expressions, of the BLL. 94The same holds for using BLL bind expressions in the BB. 95The libraries can coexist, however, as 96the names of the BB library are in <code class="literal">boost</code> namespace, 97whereas the BLL names are in <code class="literal">boost::lambda</code> namespace. 98</p> 99<p> 100The BLL requires a compiler that is reasonably conformant to the 101C++ standard, whereas the BB library is more portable, and works with 102a larger set of compilers. 103</p> 104<p> 105The following two sections describe what are the semantic differences 106between the bind expressions in BB and BLL. 107</p> 108<div class="section"> 109<div class="titlepage"><div><div><h4 class="title"> 110<a name="id-1.3.21.10.3.6"></a>First argument of bind expression</h4></div></div></div> 111 112In BB the first argument of the bind expression, the target function, 113is treated differently from the other arguments, 114as no argument substitution takes place within that argument. 115In BLL the first argument is not a special case in this respect. 116 117For example: 118 119<pre class="programlisting"> 120template<class F> 121int foo(const F& f) { 122 int x; 123 .. 124 bind(f, _1)(x); 125 ... 126} 127</pre> 128<pre class="programlisting"> 129int bar(int, int); 130nested(bind(bar, 1, _1)); 131</pre> 132 133The bind expression inside <code class="literal">foo</code> becomes: 134<pre class="programlisting"> 135bind(bind(bar, 1, _1), _1)(x) 136</pre> 137 138The BLL interpretes this as: 139<pre class="programlisting"> 140bar(1, x)(x) 141</pre> 142whereas the BB library as 143<pre class="programlisting"> 144bar(1, x) 145</pre> 146 147To get this functionality in BLL, the bind expression inside the <code class="literal">foo</code> function can be written as: 148<pre class="programlisting"> 149bind(unlambda(f), _1)(x); 150</pre> 151as explained in <a class="xref" href="le_in_details.html#lambda.unlambda" title="Unlambda">the section called “Unlambda”</a>. 152 153</div> 154<p> 155The BB library supports up to nine placeholders, while the BLL 156defines only three placeholders. 157The rationale for not providing more, is that the highest arity of the 158function objects accepted by any STL algorithm is two. 159The placeholder count is easy to increase in the BB library. 160In BLL it is possible, but more laborous. 161The BLL currently passes the actual arguments to the lambda functors 162internally just as they are and does not wrap them inside a tuple object. 163The reason for this is that some widely used compilers are not capable 164of optimizing the intermediate tuple objects away. 165The creation of the intermediate tuples would cause a significant 166performance hit, particularly for the simplest (and thus the most common) 167lambda functors. 168We are working on a hybrid approach, which will allow more placeholders 169but not compromise the performance of simple lambda functors. 170</p> 171</div> 172</div> 173<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 174<td align="left"></td> 175<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 176 Software License, Version 1.0. (See accompanying file 177 <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> 178</div></td> 179</tr></table> 180<hr> 181<div class="spirit-nav"> 182<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> 183</div> 184</body> 185</html> 186