Exploring generic lambdas and lambda templates
Lambdas, which are formally called lambda expressions, are a simplified way to define function objects in the place where they are needed. This typically includes predicates or comparison functions passed to algorithms. Although we will not discuss lambda expressions in general, let's take a look at the following examples:
int arr[] = { 1,6,3,8,4,2,9 }; std::sort( std::begin(arr), std::end(arr), [](int const a, int const b) {return a > b; }); int pivot = 5; auto count = std::count_if( std::begin(arr), std::end(arr), [pivot](int const a) {return a > pivot; });
Lambda expressions are syntactic sugar, a simplified way of defining anonymous function objects. When encountering a lambda expression, the compiler generates a class with a function-call operator. For the previous example, these could look as follows:
struct __lambda_1 { ...