diff --git a/doc/apa.html b/doc/apa.html index ef4ab5c..44b5811 100644 --- a/doc/apa.html +++ b/doc/apa.html @@ -5,7 +5,7 @@ The Boost Lambda Library">

A. Rationale for some of the design decisions

1. + The Boost Lambda Library">

A. Rationale for some of the design decisions

1. Lambda functor arity

The highest placeholder index in a lambda expression determines the arity of the resulting function object. diff --git a/doc/ar01s02.html b/doc/ar01s02.html index ad318da..3a36b91 100644 --- a/doc/ar01s02.html +++ b/doc/ar01s02.html @@ -8,7 +8,7 @@ The Boost Lambda Library">

2. Getting Started

2.1. Installing the library

+ The Boost Lambda Library">

2. Getting Started

2.1. Installing the library

The library consists of include files only, hence there is no installation procedure. The boost include directory must be on the include path. @@ -44,7 +44,7 @@ Cast expressions Tuple [tuple] and the type_traits [type_traits] libraries, and on the boost/ref.hpp header.

All definitions are placed in the namespace boost::lambda and its subnamespaces. -

2.2. Conventions used in this document

In most code examples, we omit the namespace prefixes for names in the std and boost::lambda namespaces. +

2.2. Conventions used in this document

In most code examples, we omit the namespace prefixes for names in the std and boost::lambda namespaces. Implicit using declarations

 using namespace std;
diff --git a/doc/ar01s03.html b/doc/ar01s03.html
index b7dd15f..cc18b38 100644
--- a/doc/ar01s03.html
+++ b/doc/ar01s03.html
@@ -5,7 +5,7 @@
       The Boost Lambda Library">

3. Introduction

3.1. Motivation

The Standard Template Library (STL) + The Boost Lambda Library">

3. Introduction

3.1. Motivation

The Standard Template Library (STL) [STL94], now part of the C++ Standard Library [C++98], is a generic container and algorithm library. Typically STL algorithms operate on container elements via function objects. These function objects are passed as arguments to the algorithms.

@@ -105,7 +105,7 @@ as function composition is supported implicitly.

-

3.2. Introduction to lambda expressions

+

3.2. Introduction to lambda expressions

Lambda expression are common in functional programming languages. Their syntax varies between languages (and between different forms of lambda calculus), but the basic form of a lambda expressions is: diff --git a/doc/ar01s04.html b/doc/ar01s04.html index 510738a..f055718 100644 --- a/doc/ar01s04.html +++ b/doc/ar01s04.html @@ -20,7 +20,7 @@ There are quite a lot of exceptions and special cases, but discussion of them is list<int> v(10); for_each(v.begin(), v.end(), _1 = 1);

- The expression _1 = 1 creates a lambda functor which assigns the value 1 to every element in v.[1] + The expression _1 = 1 creates a lambda functor which assigns the value 1 to every element in v.[1]

Next, we create a container of pointers and make them point to the elements in the first container v: @@ -206,7 +206,7 @@ This is to prevent pointer arithmetic making non-const arrays const.

-



[1] +



[1] Strictly taken, the C++ standard defines for_each as a non-modifying sequence operation, and the function object passed to for_each should not modify its argument. The requirements for the arguments of for_each are unnecessary strict, since as long as the iterators are mutable, for_each accepts a function object that can have side-effects on their argument. Nevertheless, it is straightforward to provide another function template with the functionality ofstd::for_each but more fine-grained requirements for its arguments. diff --git a/doc/ar01s05.html b/doc/ar01s05.html index 54b1900..027267a 100644 --- a/doc/ar01s05.html +++ b/doc/ar01s05.html @@ -10,7 +10,6 @@ This section describes different categories of lambda expressions in details. We devote a separate section for each of the possible forms of a lambda expression. -

5.1. Placeholders

The BLL defines three placeholder types: placeholder1_type, placeholder2_type and placeholder3_type. BLL has a predefined placeholder variable for each placeholder type: _1, _2 and _3. @@ -78,7 +77,7 @@ For example, the following is a valid lambda expression:

cout << _1, _2[_3] = _1 && false

However, there are some restrictions that originate from the C++ operator overloading rules, and some special cases. -

5.2.1. Operators that cannot be overloaded

+

5.2.1. Operators that cannot be overloaded

Some operators cannot be overloaded at all (::, ., .*). For some operators, the requirements on return types prevent them to be overloaded to create lambda functors. These operators are ->., ->, new, new[], delete, delete[] and ?: (the conditional operator). @@ -243,6 +242,7 @@ This creates some asymmetry between the lambda functor and the original member f class A { int i; mutable int j; public: + A(int ii, int jj) : i(ii), j(jj) {}; void set_i(int x) { i = x; }; void set_j(int x) const { j = x; }; @@ -281,7 +281,27 @@ A a(0,0); bind(&A::set_i, _1, 1)(a); // a.i == 1 bind(&A::set_j, _1, 1)(a); // a.j == 1 -

5.3.3. Function objects as targets

+

5.3.3. Member variables as targets

+A pointer to a member variable is not really a function, but +the first argument to the bind function can nevertheless +be a pointer to a member variable. +Invoking such a bind expression returns a reference to the data member. +For example: + +

+struct A { int data; };
+A a;
+bind(&A::data, _1)(a) = 1;     // a.data == 1
+
+ +The cv-qualifiers of the object whose member is accessed are respected. +For example, the following tries to write into a const location: +
+const A ca = a;
+bind(&A::data, _1)(ca) = 1;     // error
+
+ +

5.3.4. Function objects as targets

Function objects, that is, class objects which have the function call operator defined, can be used as target functions. @@ -291,7 +311,7 @@ In general, BLL cannot deduce the return type of an arbitrary function object. However, there is a method for giving BLL this capability for a certain function object class. -

The sig template

+

The sig template

To make BLL aware of the return type(s) of a function object one needs to provide a member template struct sig<Args> with a typedef @@ -513,7 +533,7 @@ By using var to make index a lambda expression, we get the des In sum, var(x) creates a nullary lambda functor, which stores a reference to the variable x. When the lambda functor is invoked, a reference to x is returned. -

Naming delayed constants and variables

+

Naming delayed constants and variables

It is possible to predefine and name a delayed variable or constant outside a lambda expression. The templates var_type, constant_type and constant_ref_type serve for this purpose. @@ -542,7 +562,7 @@ Here is an example of naming a delayed constant: constant_type<char>::type space(constant(' ')); for_each(a.begin(),a.end(), cout << space << _1); -

About assignment and subscript operators

+

About assignment and subscript operators

As described in Section 5.2.2, assignment and subscripting operators are always defined as member functions. This means, that for expressions of the form x = y or x[y] to be interpreted as lambda expressions, the left-hand operand x must be a lambda expression. @@ -829,7 +849,7 @@ objects related to creating and destroying objects, showing the expression to create and call the function object, and the effect of evaluating that expression. -

Table 1. Construction and destruction related function objects.

Function object callWrapped expression
constructor<T>()(arg_list)T(arg_list)
destructor()(a)a.~A(), where a is of type A
destructor()(pa)pa.->A(), where pa is of type A*
new_ptr<T>()(arg_list)new T(arg_list)
new_array<T>()(sz)new T[sz]
delete_ptr()(p)delete p
delete_array()(p)delete p[]

5.9. Special lambda expressions

5.9.1. Preventing argument substitution

+

Table 1. Construction and destruction related function objects.

Function object callWrapped expression
constructor<T>()(arg_list)T(arg_list)
destructor()(a)a.~A(), where a is of type A
destructor()(pa)pa.->A(), where pa is of type A*
new_ptr<T>()(arg_list)new T(arg_list)
new_array<T>()(sz)new T[sz]
delete_ptr()(p)delete p
delete_array()(p)delete p[]

5.9. Special lambda expressions

5.9.1. Preventing argument substitution

When a lambda functor is called, the default behavior is to substitute the actual arguments for the placeholders within all subexpressions. @@ -955,7 +975,7 @@ int nested(const F& f) { } -

5.9.1.2. Protect

+

5.9.1.2. Protect

The protect function is related to unlambda. It is also used to prevent the argument substitution taking place, @@ -1060,7 +1080,7 @@ since calls to sub lambda functors are made inside the BLL, and are not affected by the non-const rvalue problem.

-

5.10. Casts, sizeof and typeid

5.10.1. +

5.10. Casts, sizeof and typeid

5.10.1. Cast expressions

The BLL defines its counterparts for the four cast expressions @@ -1089,7 +1109,7 @@ int count = 0; for_each(a.begin(), a.end(), if_then(ll_dynamic_cast<derived*>(_1), ++var(count))); -

5.10.2. Sizeof and typeid

+

5.10.2. Sizeof and typeid

The BLL counterparts for these expressions are named ll_sizeof and ll_typeid. diff --git a/doc/ar01s07.html b/doc/ar01s07.html index 6784818..8eb44a3 100644 --- a/doc/ar01s07.html +++ b/doc/ar01s07.html @@ -5,7 +5,7 @@ The Boost Lambda Library">

7. Practical considerations

7.1. Performance

In theory, all overhead of using STL algorithms and lambda functors + The Boost Lambda Library">

7. Practical considerations

7.1. Performance

In theory, all overhead of using STL algorithms and lambda functors compared to hand written loops can be optimized away, just as the overhead from standard STL function objects and binders can. @@ -97,7 +97,7 @@ The running times are expressed in arbitrary units." border="1"><

Some additional performance testing with an earlier version of the library is described [Jär00]. -

7.2. About compiling

The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates. +

7.2. About compiling

The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates. This has (at least) three implications:

  • While it is possible to write incredibly complex lambda expressions, it probably isn't a good idea. @@ -111,7 +111,7 @@ This can make the error messages very long and difficult to interpret, particula The C++ Standard suggests a template nesting level of 17 to help detect infinite recursion. Complex lambda templates can easily exceed this limit. Most compilers allow a greater number of nested templates, but commonly require the limit explicitly increased with a command line argument. -

7.3. Portability

+

7.3. Portability

The BLL works with the following compilers, that is, the compilers are capable of compiling the test cases that are included with the BLL:

  • GCC 3.0.4 @@ -120,7 +120,7 @@ The BLL works with the following compilers, that is, the compilers are capable o )
-

7.3.1. Test coverage

The following list describes the test files included and the features that each file covers: +

7.3.1. Test coverage

The following list describes the test files included and the features that each file covers:

-

3.2. Introduction to lambda expressions

+

3.2. Introduction to lambda expressions

Lambda expression are common in functional programming languages. Their syntax varies between languages (and between different forms of lambda calculus), but the basic form of a lambda expressions is: @@ -254,7 +254,7 @@ There are quite a lot of exceptions and special cases, but discussion of them is list<int> v(10); for_each(v.begin(), v.end(), _1 = 1); - The expression _1 = 1 creates a lambda functor which assigns the value 1 to every element in v.[1] + The expression _1 = 1 creates a lambda functor which assigns the value 1 to every element in v.[1]

Next, we create a container of pointers and make them point to the elements in the first container v: @@ -445,7 +445,6 @@ This section describes different categories of lambda expressions in details. We devote a separate section for each of the possible forms of a lambda expression. -

5.1. Placeholders

The BLL defines three placeholder types: placeholder1_type, placeholder2_type and placeholder3_type. BLL has a predefined placeholder variable for each placeholder type: _1, _2 and _3. @@ -513,7 +512,7 @@ For example, the following is a valid lambda expression:

cout << _1, _2[_3] = _1 && false

However, there are some restrictions that originate from the C++ operator overloading rules, and some special cases. -

5.2.1. Operators that cannot be overloaded

+

5.2.1. Operators that cannot be overloaded

Some operators cannot be overloaded at all (::, ., .*). For some operators, the requirements on return types prevent them to be overloaded to create lambda functors. These operators are ->., ->, new, new[], delete, delete[] and ?: (the conditional operator). @@ -678,6 +677,7 @@ This creates some asymmetry between the lambda functor and the original member f class A { int i; mutable int j; public: + A(int ii, int jj) : i(ii), j(jj) {}; void set_i(int x) { i = x; }; void set_j(int x) const { j = x; }; @@ -716,7 +716,27 @@ A a(0,0); bind(&A::set_i, _1, 1)(a); // a.i == 1 bind(&A::set_j, _1, 1)(a); // a.j == 1 -

5.3.3. Function objects as targets

+

5.3.3. Member variables as targets

+A pointer to a member variable is not really a function, but +the first argument to the bind function can nevertheless +be a pointer to a member variable. +Invoking such a bind expression returns a reference to the data member. +For example: + +

+struct A { int data; };
+A a;
+bind(&A::data, _1)(a) = 1;     // a.data == 1
+
+ +The cv-qualifiers of the object whose member is accessed are respected. +For example, the following tries to write into a const location: +
+const A ca = a;
+bind(&A::data, _1)(ca) = 1;     // error
+
+ +

5.3.4. Function objects as targets

Function objects, that is, class objects which have the function call operator defined, can be used as target functions. @@ -726,7 +746,7 @@ In general, BLL cannot deduce the return type of an arbitrary function object. However, there is a method for giving BLL this capability for a certain function object class. -

The sig template

+

The sig template

To make BLL aware of the return type(s) of a function object one needs to provide a member template struct sig<Args> with a typedef @@ -948,7 +968,7 @@ By using var to make index a lambda expression, we get the des In sum, var(x) creates a nullary lambda functor, which stores a reference to the variable x. When the lambda functor is invoked, a reference to x is returned. -

Naming delayed constants and variables

+

Naming delayed constants and variables

It is possible to predefine and name a delayed variable or constant outside a lambda expression. The templates var_type, constant_type and constant_ref_type serve for this purpose. @@ -977,7 +997,7 @@ Here is an example of naming a delayed constant: constant_type<char>::type space(constant(' ')); for_each(a.begin(),a.end(), cout << space << _1); -

About assignment and subscript operators

+

About assignment and subscript operators

As described in Section 5.2.2, assignment and subscripting operators are always defined as member functions. This means, that for expressions of the form x = y or x[y] to be interpreted as lambda expressions, the left-hand operand x must be a lambda expression. @@ -1264,7 +1284,7 @@ objects related to creating and destroying objects, showing the expression to create and call the function object, and the effect of evaluating that expression. -

Table 1. Construction and destruction related function objects.

Function object callWrapped expression
constructor<T>()(arg_list)T(arg_list)
destructor()(a)a.~A(), where a is of type A
destructor()(pa)pa.->A(), where pa is of type A*
new_ptr<T>()(arg_list)new T(arg_list)
new_array<T>()(sz)new T[sz]
delete_ptr()(p)delete p
delete_array()(p)delete p[]

5.9. Special lambda expressions

5.9.1. Preventing argument substitution

+

Table 1. Construction and destruction related function objects.

Function object callWrapped expression
constructor<T>()(arg_list)T(arg_list)
destructor()(a)a.~A(), where a is of type A
destructor()(pa)pa.->A(), where pa is of type A*
new_ptr<T>()(arg_list)new T(arg_list)
new_array<T>()(sz)new T[sz]
delete_ptr()(p)delete p
delete_array()(p)delete p[]

5.9. Special lambda expressions

5.9.1. Preventing argument substitution

When a lambda functor is called, the default behavior is to substitute the actual arguments for the placeholders within all subexpressions. @@ -1390,7 +1410,7 @@ int nested(const F& f) { } -

5.9.1.2. Protect

+

5.9.1.2. Protect

The protect function is related to unlambda. It is also used to prevent the argument substitution taking place, @@ -1495,7 +1515,7 @@ since calls to sub lambda functors are made inside the BLL, and are not affected by the non-const rvalue problem.

-

5.10. Casts, sizeof and typeid

5.10.1. +

5.10. Casts, sizeof and typeid

5.10.1. Cast expressions

The BLL defines its counterparts for the four cast expressions @@ -1524,7 +1544,7 @@ int count = 0; for_each(a.begin(), a.end(), if_then(ll_dynamic_cast<derived*>(_1), ++var(count))); -

5.10.2. Sizeof and typeid

+

5.10.2. Sizeof and typeid

The BLL counterparts for these expressions are named ll_sizeof and ll_typeid. @@ -1797,7 +1817,7 @@ public: Note, that we are reusing the existing specializations for the BLL return_type_2 template, which require that the argument types are references. -

Table 2. Action types

+arithmetic_action<plus_action>
-arithmetic_action<minus_action>
*arithmetic_action<multiply_action>
/arithmetic_action<divide_action>
%arithmetic_action<remainder_action>
+unary_arithmetic_action<plus_action>
-unary_arithmetic_action<minus_action>
&bitwise_action<and_action>
|bitwise_action<or_action>
~bitwise_action<not_action>
^bitwise_action<xor_action>
<<bitwise_action<leftshift_action_no_stream>
>>bitwise_action<rightshift_action_no_stream>
&&logical_action<and_action>
||logical_action<or_action>
!logical_action<not_action>
<relational_action<less_action>
>relational_action<greater_action>
<=relational_action<lessorequal_action>
>=relational_action<greaterorequal_action>
==relational_action<equal_action>
!=relational_action<notequal_action>
+=arithmetic_assignment_action<plus_action>
-=arithmetic_assignment_action<minus_action>
*=arithmetic_assignment_action<multiply_action>
/=arithmetic_assignment_action<divide_action>
%=arithmetic_assignment_action<remainder_action>
&=bitwise_assignment_action<and_action>
=|bitwise_assignment_action<or_action>
^=bitwise_assignment_action<xor_action>
<<=bitwise_assignment_action<leftshift_action>
>>=bitwise_assignment_action<rightshift_action>
++pre_increment_decrement_action<increment_action>
--pre_increment_decrement_action<decrement_action>
++post_increment_decrement_action<increment_action>
--post_increment_decrement_action<decrement_action>
&other_action<address_of_action>
*other_action<contents_of_action>
,other_action<comma_action>

7. Practical considerations

7.1. Performance

In theory, all overhead of using STL algorithms and lambda functors +

Table 2. Action types

+arithmetic_action<plus_action>
-arithmetic_action<minus_action>
*arithmetic_action<multiply_action>
/arithmetic_action<divide_action>
%arithmetic_action<remainder_action>
+unary_arithmetic_action<plus_action>
-unary_arithmetic_action<minus_action>
&bitwise_action<and_action>
|bitwise_action<or_action>
~bitwise_action<not_action>
^bitwise_action<xor_action>
<<bitwise_action<leftshift_action_no_stream>
>>bitwise_action<rightshift_action_no_stream>
&&logical_action<and_action>
||logical_action<or_action>
!logical_action<not_action>
<relational_action<less_action>
>relational_action<greater_action>
<=relational_action<lessorequal_action>
>=relational_action<greaterorequal_action>
==relational_action<equal_action>
!=relational_action<notequal_action>
+=arithmetic_assignment_action<plus_action>
-=arithmetic_assignment_action<minus_action>
*=arithmetic_assignment_action<multiply_action>
/=arithmetic_assignment_action<divide_action>
%=arithmetic_assignment_action<remainder_action>
&=bitwise_assignment_action<and_action>
=|bitwise_assignment_action<or_action>
^=bitwise_assignment_action<xor_action>
<<=bitwise_assignment_action<leftshift_action>
>>=bitwise_assignment_action<rightshift_action>
++pre_increment_decrement_action<increment_action>
--pre_increment_decrement_action<decrement_action>
++post_increment_decrement_action<increment_action>
--post_increment_decrement_action<decrement_action>
&other_action<address_of_action>
*other_action<contents_of_action>
,other_action<comma_action>

7. Practical considerations

7.1. Performance

In theory, all overhead of using STL algorithms and lambda functors compared to hand written loops can be optimized away, just as the overhead from standard STL function objects and binders can. @@ -1889,7 +1909,7 @@ The running times are expressed in arbitrary units." border="1"><

Some additional performance testing with an earlier version of the library is described [Jär00]. -

7.2. About compiling

The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates. +

7.2. About compiling

The BLL uses templates rather heavily, performing numerous recursive instantiations of the same templates. This has (at least) three implications:

  • While it is possible to write incredibly complex lambda expressions, it probably isn't a good idea. @@ -1903,7 +1923,7 @@ This can make the error messages very long and difficult to interpret, particula The C++ Standard suggests a template nesting level of 17 to help detect infinite recursion. Complex lambda templates can easily exceed this limit. Most compilers allow a greater number of nested templates, but commonly require the limit explicitly increased with a command line argument. -

7.3. Portability

+

7.3. Portability

The BLL works with the following compilers, that is, the compilers are capable of compiling the test cases that are included with the BLL:

  • GCC 3.0.4 @@ -1912,7 +1932,7 @@ The BLL works with the following compilers, that is, the compilers are capable o )
-

7.3.1. Test coverage

The following list describes the test files included and the features that each file covers: +

7.3.1. Test coverage

The following list describes the test files included and the features that each file covers:

  • bind_tests_simple.cpp : Bind expressions of different arities and types of target functions: function pointers, function objects and member functions. @@ -1958,7 +1978,7 @@ Contains several user defined operators and the corresponding specializations fo Contains tests for using boost::function together with lambda functors.

-

8. Relation to other Boost libraries

8.1. Boost Function

Sometimes it is convenient to store lambda functors in variables. +

8. Relation to other Boost libraries

8.1. Boost Function

Sometimes it is convenient to store lambda functors in variables. 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. The Boost Function library [function] defines wrappers for arbitrary function objects, for example lambda functors; and these wrappers have types that are easy to type out. @@ -1997,7 +2017,7 @@ delete sum; counter(3); // error, *sum does not exist anymore -

8.2. Boost Bind

+

8.2. Boost Bind

The Boost Bind [bind] library has partially overlapping functionality with the BLL. Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL. There are, however, some semantical differerences. @@ -2016,7 +2036,7 @@ a larger set of compilers.

The following two sections describe what are the semantic differences between the bind expressions in BB and BLL. -

8.2.1. First argument of bind expression

+

8.2.1. First argument of bind expression

In BB the first argument of the bind expression, the target function, is treated differently from the other arguments, @@ -2074,7 +2094,7 @@ performance hit, particularly for the simplest (and thus the most common) lambda functors. We are working on a hybrid approach, which will allow more placeholders but not compromise the performance of simple lambda functors. -

9. Contributors

+

9. Contributors

The main body of the library was written by Jaakko Järvi and Gary Powell. We've got outside help, suggestions and ideas from Jeremy Siek, Peter Higley, Peter Dimov, Valentin Bonnard, William Kempf. @@ -2082,7 +2102,7 @@ We would particularly like to mention Joel de Guzmann and his work with Phoenix which has influenced BLL significantly, making it considerably simpler to extend the library with new features. -

A. Rationale for some of the design decisions

1. +

A. Rationale for some of the design decisions

1. Lambda functor arity

The highest placeholder index in a lambda expression determines the arity of the resulting function object. @@ -2135,7 +2155,7 @@ the error would go unnoticed. Furthermore, weak arity checking simplifies the implementation a bit. Following the recommendation of the Boost review, strict arity checking was dropped. -

Bibliography

[STL94] A. A. Stepanov and M. Lee. The Standard Template Library. Hewlett-Packard Laboratories. 1994. +

Bibliography

[STL94] A. A. Stepanov and M. Lee. The Standard Template Library. Hewlett-Packard Laboratories. 1994. www.hpl.hp.com/techreports .

[SGI02] The SGI Standard Template Library. 2002. www.sgi.com/tech/stl/.

[C++98] International Standard, Programming Languages – C++. ISO/IEC:14882. 1998.

[Jär99] 2002.

[bind] Boost Bind Library. www.boost.org/libs/bind/bind.html . 2002.

[function] Boost Function Library. www.boost.org/libs/function/ . 2002.

[fc++] The FC++ library: Functional Programming in C++. Yannis Smaragdakis. Brian McNamara. www.cc.gatech.edu/~yannis/fc++/ -. 2002.



[1] +. 2002.



[1] Strictly taken, the C++ standard defines for_each as a non-modifying sequence operation, and the function object passed to for_each should not modify its argument. The requirements for the arguments of for_each are unnecessary strict, since as long as the iterators are mutable, for_each accepts a function object that can have side-effects on their argument. Nevertheless, it is straightforward to provide another function template with the functionality ofstd::for_each but more fine-grained requirements for its arguments.