MS VC2010 lambda 表达式中似乎有错误?

发布于 2024-09-10 18:02:03 字数 617 浏览 2 评论 0原文

静态初始值设定项中使用的 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);
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-09-17 18:02:03

我在最终草案中看不到任何会导致这种行为的预期(特别是因为它悄然发生)。

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


更新:我已提交此错误并得到响应:

感谢您提交此问题。这是我们 lambda 实现中的一个错误,现已修复。该修复应该会在 Visual Studio 的下一版本(可能还有 Visual Studio 2010 SP1,但我不能保证)中提供。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文