In C++ you can do this :
struct A
{
int a, b;
double k = 3; // OK
};
but not this
struct A
{
int a, b;
auto f = [this]() {return this->a + this->b; }; // Error
};
The compiler informs us of the error
non-static data member declared ‘auto’
to let us know that this would work
struct A
{
int a, b;
function<int(void)> f = [this]() { return this->a + this->b; }; // OK
};
I'd like to ask for some insight on this. Specifically the why (the easy answer ofourse would be that the standard explicitly does not allow it according to some verse)
To put it differently : Doesn't in class member initialization contain enough on the type of the member?
I'm aware that in class member initialization is syntactic sugar but is this a case where the compiler could harvest addtional info, but fails to do so due to design choices? (much like the lack of auto
prior to C++11) or a limitation set by deeper choices (like the language being statically typed etc) ?