Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Chapter 19. Boost.Lambda

Jaakko Järvi

Use, modification and distribution is subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at

Table of Contents

In a nutshell
Getting Started
Installing the library
Conventions used in this document
Introduction to lambda expressions
Using the library
Introductory Examples
Parameter and return types of lambda functors
About actual arguments to lambda functors
Storing bound arguments in lambda functions
Lambda expressions in details
Operator expressions
Bind expressions
Overriding the deduced return type
Delaying constants and variables
Lambda expressions for control structures
Construction and destruction
Special lambda expressions
Casts, sizeof and typeid
Nesting STL algorithm invocations
Extending return type deduction system
Practical considerations
About compiling
Relation to other Boost libraries
Boost Function
Boost Bind
Rationale for some of the design decisions
Lambda functor arity

In a nutshell

The Boost Lambda Library (BLL in the sequel) is a C++ template library, which implements a form of lambda abstractions for C++. The term originates from functional programming and lambda calculus, where a lambda abstraction defines an unnamed function. The primary motivation for the BLL is to provide flexible and convenient means to define unnamed function objects for STL algorithms. In explaining what the library is about, a line of code says more than a thousand words; 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 BLL is letting you define small unnamed function objects, such as the one above, directly on the call site of an STL algorithm.


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

[SGI02] The SGI Standard Template Library. 2002.

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

[Jär99] Lecture Notes in Computer Science. 1977. Springer. 2000.

[Jär00] Jaakko Järvi. Gary Powell. The Lambda Library : Lambda Abstraction in C++. Turku Centre for Computer Science. Technical Report . 378. 2000.

[Jär01] Jaakko Järvi. Gary Powell. The Lambda Library : Lambda Abstraction in C++. Second Workshop on C++ Template Programming. Tampa Bay, OOPSLA'01. . 2001.

[Jär03] Software - Practice and Expreience. 33:259-291. 2003.

[tuple] The Boost Tuple Library. . 2002.

[type_traits] The Boost type_traits. . 2002.

[ref] Boost ref. . 2002.

[bind] Boost Bind Library. . 2002.

[function] Boost Function Library. . 2002.

[fc++] The FC++ library: Functional Programming in C++. Yannis Smaragdakis. Brian McNamara. . 2002.