A specific property of C ++ lambda expressions is to capture variables in the area in which they are declared. For example, I can use the declared and initialized variable c in a lambda function, even if "c" is not sent as an argument, but it is captured by "[]":
#include<iostream> int main () {int c=5; [c](int d){std::cout<<c+d<<'\n';}(5);}
The expected result is 10. The problem occurs when at least two variables, one of which is written and the other sent as an argument, have the same name:
#include<iostream> int main () {int c=5; [c](int c){std::cout<<c<<'\n';}(3);}
I think the 2011 standard for C ++ says that the captured variable takes precedence in the arguments of the lambda expression in case of a name match. In fact, compiling the code using GCC 4.8.1 on Linux means I get the expected 5 . If I compile the same code using the apple version of the clang compiler (clang-503.0.40, the one that comes with Xcode 5.1.1 on Mac OS X 10.9.4), I get a different answer 3 .
I am trying to understand why this is happening; is it just an Apple compiler error (if the standard for the language really says that the captured "c" takes precedence) or something like that? Can this problem be fixed?
EDIT
My teacher sent an email to the GCC help desk and they replied that this was clearly a GCC compiler error and report it to Bugzilla. So Clan behavior is right!
gcc lambda c ++ 11 clang macos
pier94
source share