Skip to main content
A generic lambda is a lambda expression whose function call operator is a member template. This occurs when the lambda uses auto type specifiers in its parameter list (introduced in C++14) or defines an explicit template parameter list (introduced in C++20). It instructs the compiler to synthesize a closure type containing a function call operator member template (operator()).

Syntax Visualization

C++14: Implicit Template Parameters Using auto in the parameter list implicitly makes the lambda generic. Each auto parameter represents a distinct, unnamed template type parameter.
C++20: Explicit Template Parameter Lists C++20 introduced the ability to explicitly declare template parameters within angle brackets <> immediately following the lambda introducer [].

Compiler Translation (Under the Hood)

When the compiler encounters a generic lambda, it synthesizes an unnamed closure class. The generic parameters are translated into a templated operator(). The C++14 generic lambda [](auto x, auto y) { return x + y; } is structurally equivalent to the following compiler-generated class:

Type Deduction and Qualifiers

The auto keyword in a generic lambda parameter list strictly follows template argument deduction rules. By default, auto deduces the base type by value, stripping top-level const, volatile, and reference qualifiers. To modify the parameter type, standard reference and cv-qualifiers must be explicitly applied to the auto specifier. The auto placeholder deduces the base type T, while the explicit qualifiers determine the final parameter type:

Variadic Generic Lambdas

Generic lambdas fully support parameter packs, enabling the creation of variadic closures. This is achieved by combining auto with the ellipsis ... operator.
This translates to a closure class with a variadic member function template:

C++20 Explicit Template Parameter Lists

The C++20 explicit template syntax resolves specific structural limitations of the C++14 auto syntax, primarily regarding type enforcement and type extraction. 1. Enforcing Identical Types In C++14, [](auto a, auto b) deduces two independent types. Enforcing that both arguments are of the exact same type required static_assert and std::is_same_v. C++20 allows direct enforcement:
2. Accessing the Underlying Type In C++14, extracting the type of an auto parameter for internal declarations required decltype. If the parameter was passed by reference, decltype yielded a reference type, which lacks nested types (like ::value_type), necessitating type traits like std::decay_t or std::remove_reference_t. C++20 provides direct access to the type identifier.

C++20 Constrained Generic Lambdas (Concepts)

C++20 integrates Concepts with generic lambdas, allowing developers to constrain the types that the lambda can accept. This can be applied using either the constrained auto syntax or explicit template parameter lists. Constrained auto Syntax:
Constrained Explicit Template Syntax:
If a constrained generic lambda is invoked with arguments that do not satisfy the specified concepts, the compiler rejects the invocation during overload resolution.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More