💡 TL;DR:C++20 lambda features part 1: template syntax and type constraints, plus how C++23 deducing this finally makes recursive lambdas a one-liner without std::function.
Last updated:2026-10-13
C++20 Lambda Expressions (Part 1)¶
C++20: Lambda Templates¶
C++14 supports generic lambda expressions, which lets a lambda accept parameters of any type, for example:
which gets expanded into:
struct Func {
template <typename T1, typename T2>
auto operator()(T1 a, T2 b) const {
return a + b;
}
};
But it cannot produce the following kinds of expansions:
First: the same type
Second: only std::array
Third: no deduction
C++14 lambdas are nice, but C++20 lets us achieve these cases easily. C++20 allows lambda expressions to use template type parameters in their definition; here is the basic syntax:
The following examples show its benefits.
1. Type Constraints¶
In an example like the one below, C++14 lets us write this code, but what gets passed in at the call site is not necessarily a vector, so the behavior deviates from what was intended and errors follow.
With C++20 and later, we can constrain the type:
So here comes an interview question: in C++14, how would you constrain this to a vector safely?
2. Perfect Forwarding¶
In C++20 you can use perfect forwarding very easily, for example:
So, what is the advantage over C++14? Or put another way: what does it take to implement perfect forwarding in C++14?
That's all for this section.
Recursive Lambdas with deducing this (C++23)¶
A plain lambda cannot name itself, so recursion used to mean storing the lambda in a std::function (allocation, indirection) or wrapping it in a Y-combinator (clever, unreadable). C++23 explicit object parameters — the same deducing this feature — fix this elegantly: the lambda takes itself as its first parameter:
// C++23 explicit object parameter: the lambda receives itself
auto factorial = [](this auto self, int n) -> long long {
return n <= 1 ? 1 : n * self(n - 1);
};
static_assert(factorial(10) == 3628800);
// before C++23 you needed std::function (heavy) or a Y-combinator (clever)
The self parameter is deduced at each call, so generic recursion works too: one lambda can recurse over any type, and mutual recursion between two lambdas becomes possible by passing one into the other. Because the compiler sees through self, these calls inline as if you had written a named function.
Compiler support: GCC 14+, Clang 18+, MSVC since VS 2022 17.2 — compile with -std=c++23.
FAQ¶
How do I write a recursive lambda in C++?¶
Since C++23, give the lambda an explicit object parameter: [](this auto self, int n) { ... self(n-1) ... }. Before that, use std::function, a Y-combinator, or just a named function.
Is the deducing-this recursive lambda as fast as a normal function?¶
Yes in practice: the compiler specializes the call through self and can inline it, unlike the std::function indirect call that pre-C++23 recursion required.
Does deducing this work with generic lambdas?¶
Yes. self is auto&&-deduced per call, so the same lambda recurses over different types, and two lambdas can even call each other by passing themselves along.
Related Topics¶
- Getting Started with C++20 Coroutines | C++那些事
- C++20 channel in Practice | C++那些事
- C++20 Concepts | C++那些事
- C++20 constexpr and the Comma Expression | C++那些事
- C++20 Byte Order (endian) | C++那些事
- C++20 Modules | C++那些事
- C++20 Ranges Library | C++那些事
- C++20 std::span Design and Pitfalls | C++那些事
- A Guide to C++20 New Features | C++那些事
- C++20 [[likely]] and [[unlikely]] | C++那些事