c 11 – 似乎是MS VC2010 lambda表达式中的错误?

静态初始值设定项中使用的lambda表达式的行为

神奇地依赖于在lambda体内初始化的局部变量

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);
}

最佳答案 我不能在最终草案中看到任何会导致期望这种行为的东西(特别是因为它无声地发生).

我已经在VS10中重现了这个问题,并且GCC 4.5.0中的行为正如您所期望的那样(所有变量都已初始化)所以我会说是的,这是VS10中的一个错误,您是否打开了一个错误?

更新:我已提交this bug并得到回复:

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).

点赞