捕获的变量隐藏lambda中的传递变量。如何取消隐藏? [英] captured variable hides passed variable in lambda. how to unhide?
问题描述
考虑这个例子:
int main()
{
int a = 100;
std::cout<<[=,&a](int a,int b){return a+b;}(99,1);
return 0;
}
输出 101
。
The output is 101
instead of my expectation of 100
.
我不能指定 a,=]
,因为它给出错误。
I cant specify as so [&a,=]
as it gives error.
如何避免名称隐藏并参考参数。我知道更改名称是选项,但我很好奇。也参考标准将是有帮助的
How do i avoid the name hiding and refer to the parameter. i know changing the name is the option but i'm curious. also reference to standard will be helpful
编辑:
我使用gcc 4.7.1
i'm using gcc 4.7.1
EDIT:
这里是展示演示的ideone链接。我使用c ++ 4.7.2编译器有
ideone
here is the ideone link showing the demo. i used c++ 4.7.2 complier there
ideone
推荐答案
我在标准中找不到任何与lambdas相关的内容,这表明你的结果是预期的行为。我同意Andy的意见,这是一个在GCC的错误。 Linux上的GCC 4.7.2,MinGW的GCC 4.7.2和MinGW的GCC 4.8.0产生与问题相同的结果,但VC ++ 10和VC ++ 11产生预期的结果。
I could not find anything related to lambdas in the standard that would indicate your results are the expected behavior. I agree with Andy's comment that this is a bug in GCC. GCC 4.7.2 on Linux, GCC 4.7.2 from MinGW, and GCC 4.8.0 from MinGW produce the same results as in the question but VC++10 and VC++11 produce the expected results.
您应该考虑提交错误报告
这篇关于捕获的变量隐藏lambda中的传递变量。如何取消隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!