
C++ Lambda functions are largely used for many different purposes.
As explained in this C++ Reference document, a C++ Lambda function constructs a closure. Using lambda functions, it is possible to store a function together with its environment.
Most used form of a C++ Lambda function is highlighted in the following example:
#include "stdafx.h"
#include <iostream>
int main()
{
auto isEven = [ & ]( const int value )->bool
{
return value % 2 == 0;
};
std::cout << "Is 11 an even number? "
<< ( isEven( 11 ) ? "TRUE" : "FALSE" )
<< std::endl;
return 0;
}
Output:
Is 11 an even number? FALSE
Press any key to continue . . .
The [] characters define the captured variables. These variables can be accessed from the lambda functions as copies of the original value or by reference (using & character). These variables are optional and they can be defined in any order.
The () characters enclose the input arguments of the function. The list of parameters are optional too.
The type following the “->” indicate the trailing return-type.
C++ Lambda functions can throw exceptions too.
Given this short introduction, some examples of lambda functions usage is provided in the following sections.
How can I use lambda functions to initialize a class member variable?
#include "stdafx.h"
#include <iostream>
#include <vector>
// lambda function used to initialize a vector
auto setValues = []()->std::vector<int>
{
std::vector<int> tmp{};
for (int i = 0; i < 10; i++)
{
tmp.emplace_back(i);
}
return tmp;
};
class SomeClass
{
public:
SomeClass()
: m_data(setValues()) {}
void print()
{
for (int v : m_data)
{
std::cout << v << std::endl;
}
}
private:
const std::vector<int> m_data;
};
int main()
{
SomeClass myClass; // create a class instance
myClass.print(); // print vector values
return 0;
}
Output:
0
1
2
3
4
5
6
7
8
9
Press any key to continue . . .
In this example, a vector is initialized using a lambda function. Generally, lambda functions are used to initialize more complex data and help to implement more readable code.
Unlock now all the premium content in this page.
Sign-in or register to buy access to this content using your virtual coins.
Nevertheless, the lambda function can be used to perform additional tasks too, e.g., increase a counter value, perform other checks and complex actions. In some extent, they can be used to also mock the member function when testing the implemented code and return a desired value or throw an exception.
In addition, using the lambda function, the “FilterManager” class does not need to know about the existence of the class “OtherFilterManager”. Also, the lambda function can capture local variables. In this way, the behavior of the lambda function depends on the values of the captured variables too.
Using lambda functions to define custom tasks
The previous example can be easily modified to use lambda functions to execute customized tasks, without necessarily returning any data to the caller function.
Unlock now all the premium content in this page.
Sign-in or register to buy access to this content using your virtual coins.
Final thoughts
In this article, three different examples have been proposed to show some Lambda functions usage. In C++, the Lambda functions are a sort of Swiss Army knife. They can be used in many situations to perform any kind of action, e.g., to access some data that are private to a specific class only and process them in other parts of the code ensuring data consistency and avoiding any loss of performance.