Case 1
int main() {
int x = 100;
auto lamb_var = [y = x](){
int y = 10;
return y + 1;
};
assert (lamb_var() == 11);
return 0;
}
in https://godbolt.org/z/hPPParjnz
Both MSVC and GCC accepted shadowing the init-capture, while Clang accused y redefinition on the compound statement and throwed compiler error.
However, if we remove the init-capture and make a simple-capture, all compilers accept Shadowing:
Case 2
int main() {
int x = 100;
auto lamb_var = [x](){
int x = 10;
return x + 1;
};
assert (lamb_var() == 11);
return 0;
}
in https://godbolt.org/z/Gs4cadf5e
A simple-capture (case 2) leads to the creation of an attribute in the lambda-associated class, so shadowing should be normal.
From what I found,the expression "whose declarative region is the body of the lambda expression" of the quote below from cppreference could defend the implementation of CLANG ("redefinition"), but I'm not sure.
A capture with an initializer acts as if it declares and explicitly captures a variable declared with type auto, whose declarative region is the body of the lambda expression (that is, it is not in scope within its initializer), except that: [...]
Who is right in implementation (GCC and MSVC or Clang), and how to understand this citation of cppreference?
Related questions
Lambda capture and parameter with same name - who shadows the other? (clang vs gcc)