What is the need of template lambda introduced in C++20 when C++14 already has generic lambda?

C++14 generic lambdas are a very cool way to generate a functor with an operator () that looks like this:

template <class T, class U>
auto operator()(T t, U u) const;

But not like this:

template <class T>
auto operator()(T t1, T t2) const; // Same type please

Nor like this:

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

Nor like this (although this gets a bit tricky to actually use):

template <class T>
auto operator()() const; // No deduction

C++14 lambdas are fine, but C++20 allows us to implement these cases without hassle.


Since you can use templated lambdas in C++20, you can restrict your types in an easier way than an SFINAE expression:

auto lambda = []<typename T>(std::vector<T> t){};

This lambda will work only with vector types.


The proposal that was accepted into C++20 has a lengthy motivation section, with examples. The premise of it is this:

There are a few key reasons why the current syntax for defining generic lambdas is deemed insufficient by the author. The gist of it is that some things that can be done easily with normal function templates require significant hoop jumping to be done with generic lambdas, or can’t be done at all.The author thinks that lambdas are valuable enough that C++ should support them just as well as normal function templates.

Following that are quite a few examples.