The following code does not compile, giving an error "invalid use of non-static data member"
struct A {
int m_i;
void f() const;
};
void A::f() const
{
auto l = [&](int x = m_i) { };
}
int main()
{
A a{1};
}
Now, I can understand that in a definition of a method of A one cannot use a non-static field as default parameter, because they are not know at the place of the definition. Indeed, in general default arguments cannot be non-static.
However, in the code above, the lambda l
is defined inside a non-static method A::f()
, so it seems to me that in the definition it should be able to access to the non-static member A::m_i
.
So the question is: why this limitation in the language? What would be the problem in allowing a lambda to have default arguments that are non-static? Provided that the lambda is defined in a non-static environment?
Note: This question is not yet another question on "why this code does not compile"... it is more about, if there is some subtle reason why the language could not be allowed to consider a code like above.