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.
CodePudding user response:
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.