Member-only story
Mastering Modern C++: Lambda Evolution in C++ 14, C++ 17 and C++ 20
I. Background
Lambdas are one of the most popular features of modern C++. Since their introduction in C++ 11, they have been ubiquitous in C++ code. Moreover, since their appearance in C++ 11, they have evolved and gained important functionality. Some of these features help to write more expressive code, and since lambdas are now very common, it is well worth taking the time to learn what can be done with them.
The goal of this article is to analyze the main evolution process of lambda, but not all the small details. If you don’t understand the basics of lambda, you can read another article on blogger’s for a detailed introduction.
The evolution of lambdas generally gives them the ability to manually define function objects.
Lambda in C++ 14
In C++ 14, lambda received four major enhancements:
- Default parameters
- Template parameters
- Generalized capture
- Return lambda from function
2.1 Default parameters
In C++ 14, lambdas can take default arguments, just like any function:
auto myLambda = [](int x, int y = 0) {
std::cout << x << '-' << y << '\n';
};std::cout << myLambda(1, 2) << '\n';
std::cout << myLambda(1) << '\n';