Imported Upstream version 1.49.0
[platform/upstream/boost.git] / doc / html / lambda / s10.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Rationale for some of the design decisions</title>
5 <link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8 <link rel="up" href="../lambda.html" title="Chapter&#160;14.&#160;Boost.Lambda">
9 <link rel="prev" href="s09.html" title="Contributors">
10 <link rel="next" href="../boost_lexical_cast.html" title="Chapter&#160;15.&#160;Boost.Lexical_Cast 1.0">
11 </head>
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
15 <td align="center"><a href="../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../more/index.htm">More</a></td>
20 </tr></table>
21 <hr>
22 <div class="spirit-nav">
23 <a accesskey="p" href="s09.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="../boost_lexical_cast.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
24 </div>
25 <div class="section">
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="id2380987"></a>Rationale for some of the design decisions</h2></div></div></div>
28 <div class="toc"><dl><dt><span class="section"><a href="s10.html#lambda.why_weak_arity">
29 Lambda functor arity
30 </a></span></dt></dl></div>
31 <div class="section">
32 <div class="titlepage"><div><div><h3 class="title">
33 <a name="lambda.why_weak_arity"></a>
34 Lambda functor arity
35 </h3></div></div></div>
36 <p>
37 The highest placeholder index in a lambda expression determines the arity of the resulting function object.
38 However, this is just the minimal arity, as the function object can take arbitrarily many arguments; those not needed are discarded.
39 Consider the two bind expressions and their invocations below:
40
41 </p>
42 <pre class="programlisting">
43 bind(g, _3, _3, _3)(x, y, z); 
44 bind(g, _1, _1, _1)(x, y, z); 
45 </pre>
46 <p>
47
48 This first line discards arguments <code class="literal">x</code> and
49 <code class="literal">y</code>, and makes the call:
50 </p>
51 <pre class="programlisting">
52 g(z, z, z) 
53 </pre>
54 <p>
55 whereas the second line discards arguments <code class="literal">y</code> and
56 <code class="literal">z</code>, and calls:
57 </p>
58 <pre class="programlisting">
59 g(x, x, x)
60 </pre>
61 <p>
62 In earlier versions of the library, the latter line resulted in a compile 
63 time error.
64
65 This is basically a tradeoff between safety and flexibility, and the issue
66 was extensively discussed during the Boost review period of the library.
67 The main points for the <span class="emphasis"><em>strict arity</em></span> checking
68 was that it might
69 catch a programming error at an earlier time and that a lambda expression that
70 explicitly discards its arguments is easy to write:
71 </p>
72 <pre class="programlisting">
73 (_3, bind(g, _1, _1, _1))(x, y, z);
74 </pre>
75 <p>
76 This lambda expression takes three arguments.
77 The left-hand argument of the comma operator does nothing, and as comma 
78 returns the result of evaluating the right-hand argument we end up with 
79 the call
80 <code class="literal">g(x, x, x)</code>
81 even with the strict arity.
82 </p>
83 <p>
84 The main points against the strict arity checking were that the need to 
85 discard arguments is commonplace, and should therefore be straightforward, 
86 and that strict arity checking does not really buy that much more safety, 
87 particularly as it is not symmetric.
88 For example, if the programmer wanted to write the expression 
89 <code class="literal">_1 + _2</code> but mistakenly wrote <code class="literal">_1 + 2</code>, 
90 with strict arity checking, the complier would spot the error.
91 However, if the erroneous expression was <code class="literal">1 + _2</code> instead,
92 the error would go unnoticed.
93 Furthermore, weak arity checking simplifies the implementation a bit.
94 Following the recommendation of the Boost review, strict arity checking 
95 was dropped.
96 </p>
97 </div>
98 </div>
99 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
100 <td align="left"></td>
101 <td align="right"><div class="copyright-footer">Copyright &#169; 1999-2004 Jaakko J&#228;rvi, Gary Powell<p>Use, modification and distribution is subject to the Boost
102     Software License, Version 1.0. (See accompanying file
103     <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>
104 </div></td>
105 </tr></table>
106 <hr>
107 <div class="spirit-nav">
108 <a accesskey="p" href="s09.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="../boost_lexical_cast.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
109 </div>
110 </body>
111 </html>