scoping of declarations in loops and -Wshadow
Niall Smart
njs3@doc.ic.ac.uk
Tue Mar 24 08:28:00 GMT 1998
Hi,
Consider the following C++ program:
int main()
{
volatile int j;
for (int i = 0; i < 10; i++)
j = i;
#ifdef REDECLARE_I
for (int i = 0; i < 10; i++)
j = i;
#else
for (i = 0; i < 10; i++)
j = i;
#endif
}
One or other of the following warnings is bogus, I believe.
[njs3@cnet0000 ~]$ g++ -v
Reading specs from /homes/njs3/pub/egcs/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.08/specs
gcc version egcs-2.91.08 980214 (gcc-2.8.0 release)
[njs3@cnet0000 ~]$ g++ foo.c -Wall -Wshadow
foo.c: In function `int main()':
foo.c:12: warning: name lookup of `i' changed for new ANSI `for' scoping
foo.c:5: warning: using obsolete binding at `i'
[njs3@cnet0000 ~]$ g++ foo.c -Wall -Wshadow -DREDECLARE_I
foo.c: In function `int main()':
foo.c:9: warning: declaration of `i' shadows previous local
Surely they contradict each other? I think the second one is in error.
Regards,
Niall Smart
More information about the Gcc-bugs
mailing list