C++11 function definitions with auto keyword

c++, c++11, lambda

Solution

In C++11, a lambda expression can omit its return type if it can deduce the exact one without ambiguity. However, this rule doesn't apply to the regular function.

int f() { return 0; } // a legal C++ function

auto f() -> int { return 0; } // a legal C++ function only in C++11

auto f() { return 0; } // an illegal C++ function even if in C++11

Problem

I am curious about using auto keyword in C++11. for function definition, you must write the return type of function: ``` auto f (int x) -> int { return x + 3; }; //success auto f (int x) { return x + 3; }; //fail ``` but in this example the both of them will work: ``` auto f = [](int x) { return x + 3; }; //expect a failure but it works auto f = [](int x) -> int { return x + 3; }; // this is expected code ``` thanks.

Original source