Lambda auto
suggest changeauto is a convenient way to name a lambda. The type of that variable is the unique closure type, not a function-pointer type. A lambda that captures nothing can still be converted to a function pointer:
auto DoThis = [](int a, int b) { return a + b; };
// DoThis has a unique closure type.
int (*pDoThis)(int, int) = [](int a, int b) { return a + b; };
auto c = DoThis(1, 2); // c is int
auto d = pDoThis(1, 2); // d is int
If a lambda does not declare a return type, that type is inferred from the return expressions. In C++11 the body has to be a single return statement. C++14 deduces it in more complicated bodies as well.
These three are the same for this body. The -> auto form is C++14:
[](int a, int b) -> int { return a + b; };
[](int a, int b) -> auto { return a + b; };
[](int a, int b) { return a + b; };
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents