Let's consider this code:
int main()
{
int a = 1;
auto f1 = [a]() {
int a = 10;
return a;
};
auto f2 = []() {
int a = 100;
return a;
};
return a + f1() + f2();
}
When using flag -Wshadow with gcc (tested on 10.2), we get these warnings:
<source>:26:13: warning: declaration of 'a' shadows a lambda capture [-Wshadow]
6 | int a = 10;
<source>:21:13: warning: declaration of 'a' shadows a previous local [-Wshadow]
11 | int a = 100;
I understand the first case, where we explicitly capture a, and thus are shadowing the original local. However, the second case is interesting, because if we remove declaration int a = 100; we get a compile error (= error: 'a' is not captured: return a;). Doesn't that "prove" that the declaration is not in the same scope as the original local, and thus we are not actually shadowing anything? Hence my question, whether the warning (for the second case) is indeed valid, or whether gcc is being a bit too strict here?