Take a constructor parameter with the same identifier as the data member it's initializing. If the two are used inside an initialization list, it would be considered safe, and "shadowing" would not occur.
However, take the following example:
struct A{
A(int a, int b);
int a, b;
};
A::A(int a, int b)
: a(a)
, b(b)
{}
int main(){}
warnings seen with g++:
g++ -Wshadow --std=c++1z -o main main.cpp
main.cpp: In constructor ‘A::A(int, int)’:
main.cpp:8:18: warning: declaration of ‘b’ shadows a member of ‘A’ [-Wshadow]
A::A(int a, int b)
^
main.cpp:4:10: note: shadowed declaration is here
int a, b;
^
main.cpp:8:18: warning: declaration of ‘a’ shadows a member of ‘A’ [-Wshadow]
A::A(int a, int b)
^
main.cpp:4:7: note: shadowed declaration is here
int a, b;
warnings seen with clang:
clang++ -Wshadow --std=c++1z -o main t.cpp
main.cpp:8:10: warning: declaration shadows a field of 'A' [-Wshadow]
A::A(int a, int b)
^
main.cpp:4:7: note: previous declaration is here
int a, b;
^
main.cpp:8:17: warning: declaration shadows a field of 'A' [-Wshadow]
A::A(int a, int b)
^
main.cpp:4:10: note: previous declaration is here
int a, b;
I'm not doing anything to the data members in the body of the constructor-- so why am I getting these warnings?
I'd like to leave the warning flag enabled to catch legitimate mishaps, but these particular cases are causing too much false noise. Is there validity in these warnings?