Skip to content

💡 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:

auto func = [](auto a, auto b) {
    return a + b;
};

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

template <class T>
auto operator()(T t1, T t2) const;

Second: only std::array

template <class T, std::size_t N>
auto operator()(std::array<T, N> const &) const; 

Third: no deduction

template <class T>
auto operator()() const;

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:

[]<typename T>(){}

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.

auto print = [](auto vec) {
  for (auto x : vec) {
    std::cout << x << std::endl;
  }
};

With C++20 and later, we can constrain the type:

auto print2 = []<typename T>(std::vector<T>& vec){
    //使用 std::vector<T> 执行操作
};

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:

auto lambdaFunc = []<typename T>(T&& number) {
  processFunction(std::forward<T>(number));
};

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.

Comments