The C++ standard, clauses 3.3.2 paragraph 4, and 6.4 paragraph 3, states that names declared in the condition of `while', `if', `for' and `switch' statements, may not be redeclared in any of the outermost controlled blocks.
This does work for `while' statements, however it does not work with `if', or `switch' statements. E.g. The following are compiled, but should not: if (int foo = 0) { int foo = 1; // invalid } switch (int foo = 0) { default: int foo = 1; // invalid } A similar example with a for statement is reported in Bug #2288 -- Summary: names declared in a condition may be redeclared Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: andrew dot stubbs at st dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31956