1. Problem It Solves
C++20 lets a lambda name its template parameters, making relationships between parameter types explicit without defining a separate function template. It makes an important assumption visible and checkable.
2. Prerequisites
Generic lambdas and template argument deduction.
You should be able to compile a short program and read its output.
3. Core Idea
The angle-bracket list is the lambda’s private template header. It exposes the deduced type so the parameter list can reuse or constrain it. Read []<class T> as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
auto maximum = []<class T>(T a, T b) { return a < b ? b : a; };5. How It Works
The program introduces the smallest relevant form of
[]<class T>.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
A single named
Trequires matching deductions; calls with two different argument types can fail even when anauto, autolambda would accept them.Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when a local callable needs named template parameters, packs, or constraints.
Avoid it when independent
autoparameters already express the intended flexibility.
8. Simple Example
A lambda names T, accepts two values of that same type, and returns their larger value. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/12_explicit_template_parameters_lambda/main.cpp
// Day 12: Explicit Template Parameter Lists for Lambdas
#include <iostream>
int main() {
auto maximum = []<class T>(T a, T b) {
return a < b ? b : a;
};
std::cout << "max = " << maximum(4, 9) << '\n';
}
9. Key Takeaways
[]<class T>expresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
[]<class T>in the minimal example?Medium — Why does the call with two
intvalues compile, and what type is returned?Hard — What deduction conflict occurs for a call with
intanddoublewhen both parameters are declared asT?