Argument type auto deduction and anonymous lambda functions

Your preferred syntax is legal as of C++14, and is referred to as a generic lambda or polymorphic lambda.

http://isocpp.org/blog/2013/04/n3649-generic-polymorphic-lambda-expressions-r3

auto lambda = [](auto x) { return x; };
lambda(5);
lambda("hello");
lambda(std::vector<int>({5, 4, 3}));

As of C++20, we can also use this syntax for regular functions:

auto f(auto x) { return x; }

No. "Polymorphic lambdas" is what this feature was referred to during the C++ committee discussions, and it was not standardized. The parameter types of a lambda must be specified.

You can use decltype though:

std::for_each(ints.begin(), ints.end(), [](decltype(*ints.begin())& val){ val = 7; });

Tags:

C++

Lambda

C++11