2

I'm currently learning about generic lambda functions, and I am quite curious about the differences between:

[](auto x){}; and []<typename T>(T x){};

They both do the same thing but is one faster than the other? What's the point of having these 2 syntaxes.

tomatto
  • 149
  • 10
  • 2
    afaik they're equivalent. Also see : https://stackoverflow.com/questions/17233547/how-does-generic-lambda-work-in-c14 – Pepijn Kramer Nov 20 '21 at 17:21

1 Answers1

3

Although the two are functionally equivalent, they are the features of C++14 and C++20, namely generic lambda and template syntax for generic lambdas, which means that the latter is only well-formed in C++20.

Compared to the auto which can accept any type, the latter can make lambda accept a specific type, for example:

[]<class T>(const std::vector<T>& x){};

In addition, it also enables lambda to forward parameters in a more natural form:

[]<class... Args>(Args&&... args) {
  return f(std::forward<Args>(args)...);
};

You can get more details through the original paper P0428.

康桓瑋
  • 33,481
  • 5
  • 40
  • 90