Behaviour of a lambda expression used in static initializers
magically depends on local variables initialized inside lambda body
int static_1 =
[=]() -> int {
int k_=7;// if this statement presents, the lambda doesn't work (static_1 remains uninitialized)
return 5;
} ();
int static_2=
[=]() -> int {
//Ok without variable initializer int k_=7;
return 5;
}();
int main() {
int local=
[=]() -> int {
int k_=7; // Ok with variable initializer when lambda used in local function context
return 5;
} ();
printf("\n static_1= %d \n static_2= %d \n local= %d", static_1,static_2,local);
}
I can’t see anything in the final draft that would lead to expect this behaviour (especially since it happens silently).
I’ve reproduced the issue in VS10 and the behaviour in GCC 4.5.0 is as you would expect (all variables are initialized) so I would say yes, it’s a bug in VS10, have you opened a bug?
Update: I’ve submitted this bug and got a response: