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

  • A+

introduced generic lambdas that made it possible to write following:

auto func = [](auto a, auto b){     return a + b; }; auto Foo = func(2, 5); auto Bar = func("hello", "world"); 

It is very clear that this generic lambda func works just like a templated function func would work.

Why did the C++ committee decide to add template syntax for generic lamda?


Since you can use templated lambdas in C++20, you can restrict your types in a more easy way than SFINAE expression.

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

This lambda will work only with vector types

EDIT : consteval: It is worth mentionning that use the keyword consteval in C++20 with lambda. Reminder : consteval means that the expression will produce a constant expression in everycase, that it is not the case for constexpr lambdas / functions


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: