| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title>Using the library</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="s03.html" title="Introduction"> |
|---|
| 10 | <link rel="next" href="le_in_details.html" title="Lambda expressions in details"> |
|---|
| 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="s03.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="le_in_details.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="lambda.using_library"></a>Using the library</h3></div></div></div> |
|---|
| 28 | <div class="toc"><dl> |
|---|
| 29 | <dt><span class="section"><a href="using_library.html#lambda.introductory_examples">Introductory Examples</a></span></dt> |
|---|
| 30 | <dt><span class="section"><a href="using_library.html#lambda.parameter_and_return_types">Parameter and return types of lambda functors</a></span></dt> |
|---|
| 31 | <dt><span class="section"><a href="using_library.html#lambda.actual_arguments_to_lambda_functors">About actual arguments to lambda functors</a></span></dt> |
|---|
| 32 | <dt><span class="section"><a href="using_library.html#lambda.storing_bound_arguments">Storing bound arguments in lambda functions</a></span></dt> |
|---|
| 33 | </dl></div> |
|---|
| 34 | <p> |
|---|
| 35 | The purpose of this section is to introduce the basic functionality of the library. |
|---|
| 36 | There are quite a lot of exceptions and special cases, but discussion of them is postponed until later sections. |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | </p> |
|---|
| 40 | <div class="section" lang="en"> |
|---|
| 41 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 42 | <a name="lambda.introductory_examples"></a>Introductory Examples</h4></div></div></div> |
|---|
| 43 | <p> |
|---|
| 44 | In this section we give basic examples of using BLL lambda expressions in STL algorithm invocations. |
|---|
| 45 | We start with some simple expressions and work up. |
|---|
| 46 | First, we initialize the elements of a container, say, a <code class="literal">list</code>, to the value <code class="literal">1</code>: |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | </p> |
|---|
| 50 | <pre class="programlisting"> |
|---|
| 51 | list<int> v(10); |
|---|
| 52 | for_each(v.begin(), v.end(), _1 = 1);</pre> |
|---|
| 53 | <p> |
|---|
| 54 | |
|---|
| 55 | The expression <code class="literal">_1 = 1</code> creates a lambda functor which assigns the value <code class="literal">1</code> to every element in <code class="literal">v</code>.<sup>[<a name="id2707880" href="#ftn.id2707880">1</a>]</sup></p> |
|---|
| 56 | <p> |
|---|
| 57 | Next, we create a container of pointers and make them point to the elements in the first container <code class="literal">v</code>: |
|---|
| 58 | |
|---|
| 59 | </p> |
|---|
| 60 | <pre class="programlisting"> |
|---|
| 61 | vector<int*> vp(10); |
|---|
| 62 | transform(v.begin(), v.end(), vp.begin(), &_1);</pre> |
|---|
| 63 | <p> |
|---|
| 64 | |
|---|
| 65 | The expression <code class="literal">&_1</code> creates a function object for getting the address of each element in <code class="literal">v</code>. |
|---|
| 66 | The addresses get assigned to the corresponding elements in <code class="literal">vp</code>. |
|---|
| 67 | </p> |
|---|
| 68 | <p> |
|---|
| 69 | The next code fragment changes the values in <code class="literal">v</code>. |
|---|
| 70 | For each element, the function <code class="literal">foo</code> is called. |
|---|
| 71 | The original value of the element is passed as an argument to <code class="literal">foo</code>. |
|---|
| 72 | The result of <code class="literal">foo</code> is assigned back to the element: |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | </p> |
|---|
| 76 | <pre class="programlisting"> |
|---|
| 77 | int foo(int); |
|---|
| 78 | for_each(v.begin(), v.end(), _1 = bind(foo, _1));</pre> |
|---|
| 79 | <p> |
|---|
| 80 | The next step is to sort the elements of <code class="literal">vp</code>: |
|---|
| 81 | |
|---|
| 82 | </p> |
|---|
| 83 | <pre class="programlisting">sort(vp.begin(), vp.end(), *_1 > *_2);</pre> |
|---|
| 84 | <p> |
|---|
| 85 | |
|---|
| 86 | In this call to <code class="literal">sort</code>, we are sorting the elements by their contents in descending order. |
|---|
| 87 | </p> |
|---|
| 88 | <p> |
|---|
| 89 | Finally, the following <code class="literal">for_each</code> call outputs the sorted content of <code class="literal">vp</code> separated by line breaks: |
|---|
| 90 | |
|---|
| 91 | </p> |
|---|
| 92 | <pre class="programlisting"> |
|---|
| 93 | for_each(vp.begin(), vp.end(), cout << *_1 << '\n'); |
|---|
| 94 | </pre> |
|---|
| 95 | <p> |
|---|
| 96 | |
|---|
| 97 | Note that a normal (non-lambda) expression as subexpression of a lambda expression is evaluated immediately. |
|---|
| 98 | This may cause surprises. |
|---|
| 99 | For instance, if the previous example is rewritten as |
|---|
| 100 | </p> |
|---|
| 101 | <pre class="programlisting"> |
|---|
| 102 | for_each(vp.begin(), vp.end(), cout << '\n' << *_1); |
|---|
| 103 | </pre> |
|---|
| 104 | <p> |
|---|
| 105 | the subexpression <code class="literal">cout << '\n'</code> is evaluated immediately and the effect is to output a single line break, followed by the elements of <code class="literal">vp</code>. |
|---|
| 106 | The BLL provides functions <code class="literal">constant</code> and <code class="literal">var</code> to turn constants and, respectively, variables into lambda expressions, and can be used to prevent the immediate evaluation of subexpressions: |
|---|
| 107 | </p> |
|---|
| 108 | <pre class="programlisting"> |
|---|
| 109 | for_each(vp.begin(), vp.end(), cout << constant('\n') << *_1); |
|---|
| 110 | </pre> |
|---|
| 111 | <p> |
|---|
| 112 | These functions are described more thoroughly in <a href="le_in_details.html#lambda.delaying_constants_and_variables" title="Delaying constants and variables">the section called “Delaying constants and variables”</a></p> |
|---|
| 113 | </div> |
|---|
| 114 | <div class="section" lang="en"> |
|---|
| 115 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 116 | <a name="lambda.parameter_and_return_types"></a>Parameter and return types of lambda functors</h4></div></div></div> |
|---|
| 117 | <p> |
|---|
| 118 | During the invocation of a lambda functor, the actual arguments are substituted for the placeholders. |
|---|
| 119 | The placeholders do not dictate the type of these actual arguments. |
|---|
| 120 | The basic rule is that a lambda function can be called with arguments of any types, as long as the lambda expression with substitutions performed is a valid C++ expression. |
|---|
| 121 | As an example, the expression |
|---|
| 122 | <code class="literal">_1 + _2</code> creates a binary lambda functor. |
|---|
| 123 | It can be called with two objects of any types <code class="literal">A</code> and <code class="literal">B</code> for which <code class="literal">operator+(A,B)</code> is defined (and for which BLL knows the return type of the operator, see below). |
|---|
| 124 | </p> |
|---|
| 125 | <p> |
|---|
| 126 | C++ lacks a mechanism to query a type of an expression. |
|---|
| 127 | However, this precise mechanism is crucial for the implementation of C++ lambda expressions. |
|---|
| 128 | Consequently, BLL includes a somewhat complex type deduction system which uses a set of traits classes for deducing the resulting type of lambda functions. |
|---|
| 129 | It handles expressions where the operands are of built-in types and many of the expressions with operands of standard library types. |
|---|
| 130 | Many of the user defined types are covered as well, particularly if the user defined operators obey normal conventions in defining the return types. |
|---|
| 131 | </p> |
|---|
| 132 | <p> |
|---|
| 133 | There are, however, cases when the return type cannot be deduced. For example, suppose you have defined: |
|---|
| 134 | |
|---|
| 135 | </p> |
|---|
| 136 | <pre class="programlisting">C operator+(A, B);</pre> |
|---|
| 137 | <p> |
|---|
| 138 | |
|---|
| 139 | The following lambda function invocation fails, since the return type cannot be deduced: |
|---|
| 140 | |
|---|
| 141 | </p> |
|---|
| 142 | <pre class="programlisting">A a; B b; (_1 + _2)(a, b);</pre> |
|---|
| 143 | <p> |
|---|
| 144 | There are two alternative solutions to this. |
|---|
| 145 | The first is to extend the BLL type deduction system to cover your own types (see <a href="extending.html" title="Extending return type deduction system">the section called “Extending return type deduction system”</a>). |
|---|
| 146 | The second is to use a special lambda expression (<code class="literal">ret</code>) which defines the return type in place (see <a href="le_in_details.html#lambda.overriding_deduced_return_type" title="Overriding the deduced return type">the section called “Overriding the deduced return type”</a>): |
|---|
| 147 | |
|---|
| 148 | </p> |
|---|
| 149 | <pre class="programlisting">A a; B b; ret<C>(_1 + _2)(a, b);</pre> |
|---|
| 150 | <p> |
|---|
| 151 | For bind expressions, the return type can be defined as a template argument of the bind function as well: |
|---|
| 152 | </p> |
|---|
| 153 | <pre class="programlisting">bind<int>(foo, _1, _2);</pre> |
|---|
| 154 | </div> |
|---|
| 155 | <div class="section" lang="en"> |
|---|
| 156 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 157 | <a name="lambda.actual_arguments_to_lambda_functors"></a>About actual arguments to lambda functors</h4></div></div></div> |
|---|
| 158 | <p>A general restriction for the actual arguments is that they cannot be non-const rvalues. |
|---|
| 159 | For example: |
|---|
| 160 | |
|---|
| 161 | </p> |
|---|
| 162 | <pre class="programlisting"> |
|---|
| 163 | int i = 1; int j = 2; |
|---|
| 164 | (_1 + _2)(i, j); // ok |
|---|
| 165 | (_1 + _2)(1, 2); // error (!) |
|---|
| 166 | </pre> |
|---|
| 167 | <p> |
|---|
| 168 | |
|---|
| 169 | This restriction is not as bad as it may look. |
|---|
| 170 | Since the lambda functors are most often called inside STL-algorithms, |
|---|
| 171 | the arguments originate from dereferencing iterators and the dereferencing operators seldom return rvalues. |
|---|
| 172 | And for the cases where they do, there are workarounds discussed in |
|---|
| 173 | <a href="le_in_details.html#lambda.rvalues_as_actual_arguments" title="Rvalues as actual arguments to lambda functors">the section called “Rvalues as actual arguments to lambda functors”</a>. |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | </p> |
|---|
| 177 | </div> |
|---|
| 178 | <div class="section" lang="en"> |
|---|
| 179 | <div class="titlepage"><div><div><h4 class="title"> |
|---|
| 180 | <a name="lambda.storing_bound_arguments"></a>Storing bound arguments in lambda functions</h4></div></div></div> |
|---|
| 181 | <p> |
|---|
| 182 | |
|---|
| 183 | By default, temporary const copies of the bound arguments are stored |
|---|
| 184 | in the lambda functor. |
|---|
| 185 | |
|---|
| 186 | This means that the value of a bound argument is fixed at the time of the |
|---|
| 187 | creation of the lambda function and remains constant during the lifetime |
|---|
| 188 | of the lambda function object. |
|---|
| 189 | For example: |
|---|
| 190 | </p> |
|---|
| 191 | <pre class="programlisting"> |
|---|
| 192 | int i = 1; |
|---|
| 193 | (_1 = 2, _1 + i)(i); |
|---|
| 194 | </pre> |
|---|
| 195 | <p> |
|---|
| 196 | The comma operator is overloaded to combine lambda expressions into a sequence; |
|---|
| 197 | the resulting unary lambda functor first assigns 2 to its argument, |
|---|
| 198 | then adds the value of <code class="literal">i</code> to it. |
|---|
| 199 | The value of the expression in the last line is 3, not 4. |
|---|
| 200 | In other words, the lambda expression that is created is |
|---|
| 201 | <code class="literal">lambda x.(x = 2, x + 1)</code> rather than |
|---|
| 202 | <code class="literal">lambda x.(x = 2, x + i)</code>. |
|---|
| 203 | |
|---|
| 204 | </p> |
|---|
| 205 | <p> |
|---|
| 206 | |
|---|
| 207 | As said, this is the default behavior for which there are exceptions. |
|---|
| 208 | The exact rules are as follows: |
|---|
| 209 | |
|---|
| 210 | </p> |
|---|
| 211 | <div class="itemizedlist"><ul type="disc"> |
|---|
| 212 | <li> |
|---|
| 213 | <p> |
|---|
| 214 | |
|---|
| 215 | The programmer can control the storing mechanism with <code class="literal">ref</code> |
|---|
| 216 | and <code class="literal">cref</code> wrappers [<a href="../lambda.html#cit:boost::ref" title="[ref]"><span class="abbrev">ref</span></a>]. |
|---|
| 217 | |
|---|
| 218 | Wrapping an argument with <code class="literal">ref</code>, or <code class="literal">cref</code>, |
|---|
| 219 | instructs the library to store the argument as a reference, |
|---|
| 220 | or as a reference to const respectively. |
|---|
| 221 | |
|---|
| 222 | For example, if we rewrite the previous example and wrap the variable |
|---|
| 223 | <code class="literal">i</code> with <code class="literal">ref</code>, |
|---|
| 224 | we are creating the lambda expression <code class="literal">lambda x.(x = 2, x + i)</code> |
|---|
| 225 | and the value of the expression in the last line will be 4: |
|---|
| 226 | |
|---|
| 227 | </p> |
|---|
| 228 | <pre class="programlisting"> |
|---|
| 229 | i = 1; |
|---|
| 230 | (_1 = 2, _1 + ref(i))(i); |
|---|
| 231 | </pre> |
|---|
| 232 | <p> |
|---|
| 233 | |
|---|
| 234 | Note that <code class="literal">ref</code> and <code class="literal">cref</code> are different |
|---|
| 235 | from <code class="literal">var</code> and <code class="literal">constant</code>. |
|---|
| 236 | |
|---|
| 237 | While the latter ones create lambda functors, the former do not. |
|---|
| 238 | For example: |
|---|
| 239 | |
|---|
| 240 | </p> |
|---|
| 241 | <pre class="programlisting"> |
|---|
| 242 | int i; |
|---|
| 243 | var(i) = 1; // ok |
|---|
| 244 | ref(i) = 1; // not ok, ref(i) is not a lambda functor |
|---|
| 245 | </pre> |
|---|
| 246 | <p> |
|---|
| 247 | |
|---|
| 248 | The functions <code class="literal">ref</code> and <code class="literal">cref</code> mostly |
|---|
| 249 | exist for historical reasons, |
|---|
| 250 | and <code class="literal">ref</code> can always |
|---|
| 251 | be replaced with <code class="literal">var</code>, and <code class="literal">cref</code> with |
|---|
| 252 | <code class="literal">constant_ref</code>. |
|---|
| 253 | See <a href="le_in_details.html#lambda.delaying_constants_and_variables" title="Delaying constants and variables">the section called “Delaying constants and variables”</a> for details. |
|---|
| 254 | The <code class="literal">ref</code> and <code class="literal">cref</code> functions are |
|---|
| 255 | general purpose utility functions in Boost, and hence defined directly |
|---|
| 256 | in the <code class="literal">boost</code> namespace. |
|---|
| 257 | |
|---|
| 258 | </p> |
|---|
| 259 | </li> |
|---|
| 260 | <li><p> |
|---|
| 261 | Array types cannot be copied, they are thus stored as const reference by default. |
|---|
| 262 | </p></li> |
|---|
| 263 | <li> |
|---|
| 264 | <p> |
|---|
| 265 | For some expressions it makes more sense to store the arguments as references. |
|---|
| 266 | |
|---|
| 267 | For example, the obvious intention of the lambda expression |
|---|
| 268 | <code class="literal">i += _1</code> is that calls to the lambda functor affect the |
|---|
| 269 | value of the variable <code class="literal">i</code>, |
|---|
| 270 | rather than some temporary copy of it. |
|---|
| 271 | |
|---|
| 272 | As another example, the streaming operators take their leftmost argument |
|---|
| 273 | as non-const references. |
|---|
| 274 | |
|---|
| 275 | The exact rules are: |
|---|
| 276 | |
|---|
| 277 | </p> |
|---|
| 278 | <div class="itemizedlist"><ul type="circle"> |
|---|
| 279 | <li><p>The left argument of compound assignment operators (<code class="literal">+=</code>, <code class="literal">*=</code>, etc.) are stored as references to non-const.</p></li> |
|---|
| 280 | <li><p>If the left argument of <code class="literal"><<</code> or <code class="literal">>></code> operator is derived from an instantiation of <code class="literal">basic_ostream</code> or respectively from <code class="literal">basic_istream</code>, the argument is stored as a reference to non-const. |
|---|
| 281 | For all other types, the argument is stored as a copy. |
|---|
| 282 | </p></li> |
|---|
| 283 | <li><p> |
|---|
| 284 | In pointer arithmetic expressions, non-const array types are stored as non-const references. |
|---|
| 285 | This is to prevent pointer arithmetic making non-const arrays const. |
|---|
| 286 | |
|---|
| 287 | </p></li> |
|---|
| 288 | </ul></div> |
|---|
| 289 | </li> |
|---|
| 290 | </ul></div> |
|---|
| 291 | </div> |
|---|
| 292 | <div class="footnotes"> |
|---|
| 293 | <br><hr width="100" align="left"> |
|---|
| 294 | <div class="footnote"><p><sup>[<a name="ftn.id2707880" href="#id2707880">1</a>] </sup> |
|---|
| 295 | Strictly taken, the C++ standard defines <code class="literal">for_each</code> as a <span class="emphasis"><em>non-modifying sequence operation</em></span>, and the function object passed to <code class="literal">for_each</code> should not modify its argument. |
|---|
| 296 | The requirements for the arguments of <code class="literal">for_each</code> are unnecessary strict, since as long as the iterators are <span class="emphasis"><em>mutable</em></span>, <code class="literal">for_each</code> accepts a function object that can have side-effects on their argument. |
|---|
| 297 | Nevertheless, it is straightforward to provide another function template with the functionality of<code class="literal">std::for_each</code> but more fine-grained requirements for its arguments. |
|---|
| 298 | </p></div> |
|---|
| 299 | </div> |
|---|
| 300 | </div> |
|---|
| 301 | <table width="100%"><tr> |
|---|
| 302 | <td align="left"></td> |
|---|
| 303 | <td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td> |
|---|
| 304 | </tr></table> |
|---|
| 305 | <hr> |
|---|
| 306 | <div class="spirit-nav"> |
|---|
| 307 | <a accesskey="p" href="s03.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="le_in_details.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 308 | </div> |
|---|
| 309 | </body> |
|---|
| 310 | </html> |
|---|