Recursive call in lambda (C++11) [duplicate]-Collection of common programming errors
The reason is that there is no special case for lambda-expression initializers of auto
variables.
Such special cases would be prone to errors and misuses. You need to define the rules when you propose that something like a()
should work. How is the operator()
looked up? What is the precise state of a
‘s type? Will the type be complete? (which implies that you already know the capture list of the lambda). Once you have formulated that in a format reasonable for a spec, it would be easier to make statements on it.
Allowing your use case would mean yet another case where you need to scan ahead in code, because to determine the type of a
in a()
you must be sure that the initializer ends with nothing that could “unlambda” the type
struct y { void operator()() { } };
template y operator+(T, y) { return y(); }
auto x = [] { x(); } + y();
In this case, x()
would call y::operator()
, not the lambda.
As it is now, a
is simply forbidden to be mentioned in its entire initializer. Because in C++, auto
is not a type. It is merely a type specifier standing for a to-be-deduced type. As a consequence, an expression can never have type auto.