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. |
---|
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 | </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. |
---|
82 | Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL. |
---|
83 | There are, however, some semantical differerences. |
---|
84 | </p> |
---|
85 | <p> |
---|
86 | The BLL and BB evolved separately, and have different implementations. |
---|
87 | This means that the bind expressions from the BB cannot be used within |
---|
88 | bind expressions, or within other type of lambda expressions, of the BLL. |
---|
89 | The same holds for using BLL bind expressions in the BB. |
---|
90 | The libraries can coexist, however, as |
---|
91 | the names of the BB library are in <code class="literal">boost</code> namespace, |
---|
92 | whereas the BLL names are in <code class="literal">boost::lambda</code> namespace. |
---|
93 | </p> |
---|
94 | <p> |
---|
95 | The BLL requires a compiler that is reasonably conformant to the |
---|
96 | C++ standard, whereas the BB library is more portable, and works with |
---|
97 | a larger set of compilers. |
---|
98 | </p> |
---|
99 | <p> |
---|
100 | The following two sections describe what are the semantic differences |
---|
101 | between 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 | |
---|
107 | In BB the first argument of the bind expression, the target function, |
---|
108 | is treated differently from the other arguments, |
---|
109 | as no argument substitution takes place within that argument. |
---|
110 | In BLL the first argument is not a special case in this respect. |
---|
111 | |
---|
112 | For example: |
---|
113 | |
---|
114 | <pre class="programlisting"> |
---|
115 | template<class F> |
---|
116 | int foo(const F& f) { |
---|
117 | int x; |
---|
118 | .. |
---|
119 | bind(f, _1)(x); |
---|
120 | ... |
---|
121 | } |
---|
122 | </pre> |
---|
123 | <pre class="programlisting"> |
---|
124 | int bar(int, int); |
---|
125 | nested(bind(bar, 1, _1)); |
---|
126 | </pre> |
---|
127 | |
---|
128 | The bind expression inside <code class="literal">foo</code> becomes: |
---|
129 | <pre class="programlisting"> |
---|
130 | bind(bind(bar, 1, _1), _1)(x) |
---|
131 | </pre> |
---|
132 | |
---|
133 | The BLL interpretes this as: |
---|
134 | <pre class="programlisting"> |
---|
135 | bar(1, x)(x) |
---|
136 | </pre> |
---|
137 | whereas the BB library as |
---|
138 | <pre class="programlisting"> |
---|
139 | bar(1, x) |
---|
140 | </pre> |
---|
141 | |
---|
142 | To get this functionality in BLL, the bind expression inside the <code class="literal">foo</code> function can be written as: |
---|
143 | <pre class="programlisting"> |
---|
144 | bind(unlambda(f), _1)(x); |
---|
145 | </pre> |
---|
146 | as explained in <a href="le_in_details.html#lambda.unlambda" title="Unlambda">the section called “Unlambda”</a>. |
---|
147 | |
---|
148 | </div> |
---|
149 | <p> |
---|
150 | The BB library supports up to nine placeholders, while the BLL |
---|
151 | defines only three placeholders. |
---|
152 | The rationale for not providing more, is that the highest arity of the |
---|
153 | function objects accepted by any STL algorithm is two. |
---|
154 | The placeholder count is easy to increase in the BB library. |
---|
155 | In BLL it is possible, but more laborous. |
---|
156 | The BLL currently passes the actual arguments to the lambda functors |
---|
157 | internally just as they are and does not wrap them inside a tuple object. |
---|
158 | The reason for this is that some widely used compilers are not capable |
---|
159 | of optimizing the intermediate tuple objects away. |
---|
160 | The creation of the intermediate tuples would cause a significant |
---|
161 | performance hit, particularly for the simplest (and thus the most common) |
---|
162 | lambda functors. |
---|
163 | We are working on a hybrid approach, which will allow more placeholders |
---|
164 | but 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> |
---|