views:

95

answers:

1

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);
}
+1  A: 

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:

Thank you for submitting this issue. This was a bug in our lambda implementation and has been fixed. The fix should be available in the next release of Visual Studio (and possibly Visual Studio 2010 SP1, though I cannot guarantee that).

Motti
Not yet, but your answer has convinced me that it's a bug. I'm now going to report it to Microsoft (or maybe you'll do it better?). Thank you!
@user396672, OK I've reported it, bug ID 576999.https://connect.microsoft.com/VisualStudio/feedback/details/576999
Motti