A lambda expression (or lambda function) is a convenient way of defining an anonymous, small, and one-time use function to be used in the place right where it is needed. Lambda is particularly useful with Standard Template Library (STL), as we'll see.
Lambda expressions
How to do it...
In this section, we'll write some code in order to get familiar with lambda expressions. Although the mechanics are important, pay attention to the code readability with lambda, especially in conjunction with STL. Follow these steps:
- In this program, the lambda function gets an integer and prints it to standard output. Let's open a file named lambda_01.cpp and write the following code in it:
#include <iostream>
#include <vector>
#include <algorithm>
int main ()
{
std::vector<int> v {1, 2, 3, 4, 5, 6};
for_each (begin(v), end(v), [](int x) {std::cout << x
<< std::endl;});
return 0;
}
- In this second program, the lambda function captures a prefix by reference and prepends it to the integer in the standard output. Let's write the following code in a file called lambda_02.cpp:
#include <iostream>
#include <vector>
#include <algorithm>
int main ()
{
std::vector<int> v {1, 2, 3, 4, 5, 6};
std::string prefix ("0");
for_each (begin(v), end(v), [&prefix](int x) {std::cout
<< prefix << x << std::endl;});
return 0;
}
- Finally, we compile it with g++ lambda_02.cpp.
How it works...
In the first example, the lambda function just gets an integer as input and prints it. Note that the code is concise and readable. Lambda can capture the variables in scope by reference, &, or by value, =.
The output of the second program is as follows:

In the second example, the lambda captures the variable prefix by reference, making it visible to the lambda. Here, we captured the prefix variable by reference, but we might have captured any of the following:
- All the variables by reference [&]
- All the variables by value [=]
- Specifying what variables to capture and how to capture them [&var1, =var2]
There are cases where we have to be explicit about the type to return, as in this case:
[](int x) -> std::vector<int>{
if (x%2)
return {1, 2};
else
return {3, 4};
});
The -> std::vector<int> operator, called trailing return type, tells the compiler that this lambda will return a vector of integers.
There's more...
Lambda can be decomposed into six parts:
- Capture clause: []
- Parameter list: ()
- Mutable specification: mutable
- Exception specification: noexcept
- Trailing return type: -> type
- Body: {}
Here, 1, 2, and 6 are mandatory.
Although optional, mutable specification and exception specification are worth having a look at as they might be handy in some circumstances. The mutable specification allows a by-value parameter to be modified by the body of the lambda. A variable in the parameter list is typically captured const-by-value, so the mutable specification just removes this restriction. The second case is the exception specification, which we can use to specify the exceptions the lambda might throw.
See also
The books Effective Modern C++ by Scott Meyers and The C++ Programming Language by Bjarne Stroustrup cover these topics in great detail.