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);
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在最终草案中看不到任何会导致这种行为的预期(特别是因为它悄然发生)。
我在 VS10 中重现了这个问题,GCC 4.5.0 中的行为正如您所期望的那样(所有变量都已初始化),所以我会说是的,这是 VS10 中的一个错误,您打开了一个错误吗?
更新:我已提交此错误并得到响应:
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: