http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357
Bug #: 55357
Summary: -Wshadow warns about lambda function parameters
matching variables in outer scope
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
Similar to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30566
input:
int main() {
int x = 1;
auto const lambda = [](int x) {
return x;
};
}
output:
src/main.cpp: In lambda function:
src/main.cpp:3:30: error: declaration of 'x' shadows a previous local
[-Werror=shadow]
src/main.cpp:2:6: error: shadowed declaration is here [-Werror=shadow]
src/main.cpp: In static member function 'static int
main()::<lambda(int)>::_FUN(int)':
src/main.cpp:5:2: error: declaration of 'x' shadows a previous local
[-Werror=shadow]
src/main.cpp:2:6: error: shadowed declaration is here [-Werror=shadow]
cc1plus: all warnings being treated as errors
My lambda has an empty capture specification, so the outer x is not captured.
Note that if I change the lambda parameter's name but do not change the name of
the returned value, I get an error that "x" was not captured.
I can't decide if this is correct behavior for the warning. It would catch
errors caused by people thinking they were using the outer variables by simply
disallowing overlap, which is good. However, it's not possible to use the outer
scope variable no matter what I name my variables in the inner scope, so there
is no chance of a silent behavior change.
However, the following code should always give a warning with -Wshadow:
int main() {
int x = 1;
// Capture everything
auto const lambda = [&](int x) {
return x;
};
}
I also note that gcc warns me about the first line the lambda appears on (line
3) and the last line (line 5).