The Boost Lambda Library is a C++ template library, which implements a form of lambda abstractions for C++
The primary motivation for the Boost Lambda is to provide flexible and convenient means to define unnamed function objects for STL algorithms.
The following line outputs the elements of some STL container a separated by spaces:
for_each(a.begin(), a.end(), std::cout << _1 << ' ');
The expression std::cout << _1 << ' '
defines a unary function object.
The variable _1
is the parameter of this function, a placeholder for the actual argument. Within each iteration of for_each, the function is called with an element of a
as the actual argument. This actual argument is substituted for the placeholder, and the “body” of the function is evaluated.
The essence of Boost Lambda is letting you define small unnamed function objects, such as the one above, directly on the call site of an STL algorithm.