• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>Rationale for some of the design decisions</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="s09.html" title="Contributors">
11<link rel="next" href="../boost_lexical_cast.html" title="Chapter 21. Boost.Lexical_Cast 1.0">
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="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>
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.12"></a>Rationale for some of the design decisions</h2></div></div></div>
29<div class="toc"><dl class="toc"><dt><span class="section"><a href="s10.html#lambda.why_weak_arity">
30Lambda functor arity
31</a></span></dt></dl></div>
32<div class="section">
33<div class="titlepage"><div><div><h3 class="title">
34<a name="lambda.why_weak_arity"></a>
35Lambda functor arity
36</h3></div></div></div>
37<p>
38The highest placeholder index in a lambda expression determines the arity of the resulting function object.
39However, this is just the minimal arity, as the function object can take arbitrarily many arguments; those not needed are discarded.
40Consider the two bind expressions and their invocations below:
41
42</p>
43<pre class="programlisting">
44bind(g, _3, _3, _3)(x, y, z);
45bind(g, _1, _1, _1)(x, y, z);
46</pre>
47<p>
48
49This first line discards arguments <code class="literal">x</code> and
50<code class="literal">y</code>, and makes the call:
51</p>
52<pre class="programlisting">
53g(z, z, z)
54</pre>
55<p>
56whereas the second line discards arguments <code class="literal">y</code> and
57<code class="literal">z</code>, and calls:
58</p>
59<pre class="programlisting">
60g(x, x, x)
61</pre>
62<p>
63In earlier versions of the library, the latter line resulted in a compile
64time error.
65
66This is basically a tradeoff between safety and flexibility, and the issue
67was extensively discussed during the Boost review period of the library.
68The main points for the <span class="emphasis"><em>strict arity</em></span> checking
69was that it might
70catch a programming error at an earlier time and that a lambda expression that
71explicitly discards its arguments is easy to write:
72</p>
73<pre class="programlisting">
74(_3, bind(g, _1, _1, _1))(x, y, z);
75</pre>
76<p>
77This lambda expression takes three arguments.
78The left-hand argument of the comma operator does nothing, and as comma
79returns the result of evaluating the right-hand argument we end up with
80the call
81<code class="literal">g(x, x, x)</code>
82even with the strict arity.
83</p>
84<p>
85The main points against the strict arity checking were that the need to
86discard arguments is commonplace, and should therefore be straightforward,
87and that strict arity checking does not really buy that much more safety,
88particularly as it is not symmetric.
89For example, if the programmer wanted to write the expression
90<code class="literal">_1 + _2</code> but mistakenly wrote <code class="literal">_1 + 2</code>,
91with strict arity checking, the complier would spot the error.
92However, if the erroneous expression was <code class="literal">1 + _2</code> instead,
93the error would go unnoticed.
94Furthermore, weak arity checking simplifies the implementation a bit.
95Following the recommendation of the Boost review, strict arity checking
96was dropped.
97</p>
98</div>
99</div>
100<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
101<td align="left"></td>
102<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
103    Software License, Version 1.0. (See accompanying file
104    <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>
105</div></td>
106</tr></table>
107<hr>
108<div class="spirit-nav">
109<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>
110</div>
111</body>
112</html>
113