By coder3101

2019-01-10 10:04:39 8 Comments

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?


@Antoine Morrier 2019-01-10 10:11:06

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.

@StoryTeller 2019-01-10 10:14:18

How is consteval related to the new syntax? It's cool and all, but I don't get the relevance.

@Antoine Morrier 2019-01-10 10:14:55

It is more an information about what C++20 add to the lambda expression than an answer to the question

@Hamza.S 2019-01-10 10:20:46

The new "familiar template syntax" for lambdas introduced in C++20 makes constructs such as for_types and for_range viable and way more readable compared to C++17 alternatives.

(source: compile-time iteration with C++20 lambdas)

Another interesting thing that can be done on both C++14 and C++17 generic lambdas is directly calling operator() by explicitly passing a template parameter:


   auto l = [](auto){ };
   l.template operator()<int>(0);


  auto l = []<typename T>(){ };
  l.template operator()<int>();

The C++14 example above is quite useless: there's no way of referring to the type provided to operator() in the body of the lambda without giving the argument a name and using decltype. Additionally, we're forced to pass an argument even though we might not need it.

The C++20 example shows how T is easily accessible in the body of the lambda and that a nullary lambda can now be arbitrarily templated. This is going to be very useful for the implementation of the aforementioned compile-time constructs

@Quentin 2019-01-10 10:12:10

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.

@Sebastian Mach 2019-01-11 10:05:55

Nice and concise. Just adding this: The first (same types) can be solved by (auto a, decltype(a) b) in C++14.

@Quentin 2019-01-11 10:07:05

@SebastianMach almost. With that solution b is non-deduced, and its argument will be implicitly converted to the type of a instead.

@StoryTeller 2019-01-10 10:11:21

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.

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

6 Answered Questions

[SOLVED] Does C++11, 14, 17 or 20 introduce a standard constant for pi?

3 Answered Questions

2 Answered Questions

3 Answered Questions

[SOLVED] How to create a variadic generic lambda?

2 Answered Questions

[SOLVED] How to calculate sum using variadic generic lambda in C++?

3 Answered Questions

[SOLVED] How does generic lambda work in C++14?

  • 2013-06-21 10:57:39
  • sasha.sochka
  • 37657 View
  • 105 Score
  • 3 Answer
  • Tags:   c++ lambda auto c++14

1 Answered Questions

[SOLVED] C++14 Generic lambdas in header file

2 Answered Questions

1 Answered Questions

[SOLVED] Will C++14 support unconstrained generic functions?

Sponsored Content