This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: shadowed "for" scope variables not throwing up a warning (gcc 3.1.1/cygwin)
On 06-Aug-2002, Dylan Cuthbert <dylan@q-games.com> wrote:
> I just tried the -Wshadow option and it seems to be a bit of overkill for
> the problem I'm describing.
>
> The std boost libs don't even compile with this option enabled so the option
> is pretty much useless at this time.
Well, -Wshadow only generates warnings, not errors, unless you compile
with -Werror. Not everyone uses Boost, and not everyone uses -Werror.
So I don't agree that -Wshadow is useless.
However, on further investigation I see that the C++ standard requires
a diagnostic for examples like the one that you posted.
So g++ should be issuing an error, even without -Wshadow.
Please file a bug report (see <http://gcc.gnu.org/bugs.html>).
> My problem is more to do with the iso-compliancy of gcc's current
> implementation of scopes for the "for" statement. I was always under the
> impression that the variables created within the for statement are part of
> the body scope of the for statement, not in some kind of implicit "mini"
> scope created around the body. It seems wrong and can lead to bugs and
> confusion. I cannot think of one possible reason why the user would want to
> "overwrite" the variables created in the for statement.
Your impression is not quite correct.
The standard says
| 6.5 - Iteration statements [stmt.iter]
...
| -2- The substatement in an iteration-statement implicitly defines a
| local scope (basic.scope) which is entered and exited each time
| through the loop.
and
| 6.5.3 - The for statement [stmt.for]
|
| -1- The for statement
|
| for ( for-init-statement conditionopt ; expressionopt ) statement
|
| is equivalent to
|
| {
| for-init-statement
| while ( condition ) {
| statement
| expression ;
| }
| }
|
| except [... some exceptions that aren't revelant here ...].
So the the variables created within the for statement are not
part of the body scope of the for statement.
However, it is nevertheless an error to redeclare the same
name in the outermost scope of the loop body:
| 3.3.2 - Local scope [basic.scope.local]
| -4- Names declared in the for-init-statement, and in the condition of
| if, while, for, and switch statements are local to the if, while, for,
| or switch statement (including the controlled statement), and shall
| not be redeclared in a subsequent condition of that statement nor in
| the outermost block (or, for the if statement, any of the outermost
| blocks) of the controlled statement; see stmt.select.
Since this is a requirement on the form of the program, programs which
violate this requirement are ill-formed, and a diagnostic is required.
g++ 3.1 fails to report any diagnostic, even with -ansi -pedantic.
So it does not conform to this part of the standard.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.