From cppinsights we see how in the following code the line lambda();
is interpreted by the language:
const auto lambda = [] () mutable {};
void foo()
{
lambda();
}
One would naively think, that this line calls the lambda’s (non-const) operator()
, which in turn doesn’t compile. But, instead of this, the compiler converts the non-capturing lambda to a function pointer, calls the function pointer, and accepts the code.
What’s the purpose of this conversion? To me, it would be more logical, if this would be rejected. There is no sign, that the programmer intended this conversion. The language does this on its own.
Note, that the conversion happens only in the above case, where calling lambda.operator()()
would discard qualifiers. It does not happen (i.e., operator()
is called directly), if lambda
is not const
, or operator()
is not marked mutable
.