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
Any C++ construct that can be called with the function call syntax
is a function object.
The STL contains predefined function objects for some common cases (such as template <class T> struct plus : public binary_function<T, T, T> { T operator()(const T& i, const T& j) const { return i + j; } };
The base class
In addition to the basic function object classes, such as the one above,
the STL contains class plus_1 { int _i; public: plus_1(const int& i) : _i(i) {} int operator()(const int& j) { return _i + j; } };
the equivalent functionality can be achieved with the plus_1(1) bind1st(plus<int>(), 1)
The subexpression transform(a.begin(), a.end(), ostream_iterator<int>(cout), bind1st(plus<int>(), 1));
To make the binder templates more generally applicable, the STL contains
All these tools aim at one goal: to make it possible to specify
The Boost Lambda Library provides solutions for the problems described above: -
Unnamed functions can be created easily with an intuitive syntax. The above example can be written as: transform(a.begin(), a.end(), ostream_iterator<int>(cout), 1 + _1); or even more intuitively: for_each(a.begin(), a.end(), cout << (1 + _1)); Most of the restrictions in argument binding are removed, arbitrary arguments of practically any C++ function can be bound. Separate function composition operations are not needed, as function composition is supported implicitly.
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: lambda x A lambda expression defines an unnamed function and consists of: the parameters of this function: `x` ._{1}... x_{n}the expression e which computes the value of the function in terms of the parameters `x` ._{1}... x_{n}
A simple example of a lambda expression is lambda x y.x+y Applying the lambda function means substituting the formal parameters with the actual arguments: (lambda x y.x+y) 2 3 = 2 + 3 = 5
In the C++ version of lambda expressions the lambda x y.x+y is _1 + _2
Hence, there is no syntactic keyword for C++ lambda expressions.
The use of a placeholder as an operand implies that the operator invocation is a lambda expression.
However, this is true only for operator invocations.
Lambda expressions containing function calls, control structures, casts etc. require special syntactic constructs.
Most importantly, function calls need to be wrapped inside a lambda x y.foo(x,y)
Rather than bind(foo, _1, _2)
We refer to this type of C++ lambda expressions as A lambda expression defines a C++ function object, hence function application syntax is like calling any other function object, for instance:
A bind expression is in effect a
A lambda expression defines a function. A C++ lambda expression concretely constructs a function object, |