| [29] | 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. |
|---|
| 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 href="../lambda.html#cit:boost::function" title="[function]"><span class="abbrev">function</span></a>] defines wrappers for arbitrary function objects, for example |
|---|
| 38 | lambda functors; and these wrappers have types that are easy to type out. |
|---|
| 39 | |
|---|
| 40 | For example: |
|---|
| 41 | |
|---|
| 42 | </p> |
|---|
| 43 | <pre class="programlisting"> |
|---|
| 44 | boost::function<int(int, int)> f = _1 + _2; |
|---|
| 45 | boost::function<int&(int&)> g = (_1 += 10); |
|---|
| 46 | int i = 1, j = 2; |
|---|
| 47 | f(i, j); // returns 3 |
|---|
| 48 | g(i); // sets i to = 11; |
|---|
| 49 | </pre> |
|---|
| 50 | <p> |
|---|
| 51 | |
|---|
| 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. |
|---|
| 54 | |
|---|
| 55 | Note that storing lambda functors inside <code class="literal">boost::function</code> |
|---|
| 56 | introduces a danger. |
|---|
| 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. |
|---|
| 62 | |
|---|
| 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. |
|---|
| 65 | For example: |
|---|
| 66 | |
|---|
| 67 | </p> |
|---|
| 68 | <pre class="programlisting"> |
|---|
| 69 | int* sum = new int(); |
|---|
| 70 | *sum = 0; |
|---|
| 71 | boost::function<int&(int)> counter = *sum += _1; |
|---|
| 72 | counter(5); // ok, *sum = 5; |
|---|
| 73 | delete sum; |
|---|
| 74 | counter(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. |
|---|
| 86 | Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL. |
|---|
| 87 | There are, however, some semantical differerences. |
|---|
| 88 | </p> |
|---|
| 89 | <p> |
|---|
| 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. |
|---|
| 97 | </p> |
|---|
| 98 | <p> |
|---|
| 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. |
|---|
| 102 | </p> |
|---|
| 103 | <p> |
|---|
| 104 | The following two sections describe what are the semantic differences |
|---|
| 105 | between 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 | |
|---|
| 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. |
|---|
| 115 | |
|---|
| 116 | For example: |
|---|
| 117 | |
|---|
| 118 | <pre class="programlisting"> |
|---|
| 119 | template<class F> |
|---|
| 120 | int foo(const F& f) { |
|---|
| 121 | int x; |
|---|
| 122 | .. |
|---|
| 123 | bind(f, _1)(x); |
|---|
| 124 | ... |
|---|
| 125 | } |
|---|
| 126 | </pre> |
|---|
| 127 | <pre class="programlisting"> |
|---|
| 128 | int bar(int, int); |
|---|
| 129 | nested(bind(bar, 1, _1)); |
|---|
| 130 | </pre> |
|---|
| 131 | |
|---|
| 132 | The bind expression inside <code class="literal">foo</code> becomes: |
|---|
| 133 | <pre class="programlisting"> |
|---|
| 134 | bind(bind(bar, 1, _1), _1)(x) |
|---|
| 135 | </pre> |
|---|
| 136 | |
|---|
| 137 | The BLL interpretes this as: |
|---|
| 138 | <pre class="programlisting"> |
|---|
| 139 | bar(1, x)(x) |
|---|
| 140 | </pre> |
|---|
| 141 | whereas the BB library as |
|---|
| 142 | <pre class="programlisting"> |
|---|
| 143 | bar(1, x) |
|---|
| 144 | </pre> |
|---|
| 145 | |
|---|
| 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); |
|---|
| 149 | </pre> |
|---|
| 150 | as explained in <a href="le_in_details.html#lambda.unlambda" title="Unlambda">the section called “Unlambda”</a>. |
|---|
| 151 | |
|---|
| 152 | </div> |
|---|
| 153 | <p> |
|---|
| 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) |
|---|
| 166 | lambda functors. |
|---|
| 167 | We are working on a hybrid approach, which will allow more placeholders |
|---|
| 168 | but 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> |
|---|