Introduction
One important new feature of C++11 was lambda expressions. In C++14 and C++17, the lambda expressions got some new additions, which have made them even more powerful. But first, what is a lambda expression?
Lambda expressions or lambda functions construct closures. A closure is a very generic term for unnamed objects that can be called like functions. In order to provide such a capability in C++, such an object must implement the ()
function calling operator, with or without parameters. Constructing such an object without lambda expressions before C++11 could still look like the following:
#include <iostream> #include <string> int main() { struct name_greeter { std::string name; void operator()() { std::cout << "Hello, " << name << '\n'; } }; name_greeter greet_john_doe {"John Doe"}; greet_john_doe(); }
Instances of the name_greeter
struct obviously carry a string with them. Note that both this...