[cfe-commits] Inconsistent behavior with variable shadow

Dmitri Gribenko gribozavr at gmail.com
Tue Jun 19 08:46:24 PDT 2012


On Tue, Jun 19, 2012 at 8:31 AM, Joerg Sonnenberger
<joerg at britannica.bec.de> wrote:
> Hi all,
> consider the following input:
>
> void fn1 () { int it; for (;;) long it; }
>
> void fn2 () { for (int it;;) long it; }
>
> GCC accepts both. Clang rejects fn2, but by default silently accepts
> fn1. Is there a good reason for that mismatch?

Hello,

I think these are the relevant excerpts from C++11:

C++11 [stmt.for] p1:
... names declared in the for-init-statement are in the same
declarative-region as those declared in the condition ...

C++11 [stmt.select] p3:
A name introduced by a declaration in a condition [...] is in scope
from its point of declaration until the end of the substatements
controlled by the condition. If the name is re-declared in the
outermost block of a substatement controlled by the condition, the
declaration that re-declares the name is ill-formed. [ Example:
if (int x = f()) {
  int x; // ill-formed, redeclaration of x
}
else {
int x; // ill-formed, redeclaration of x
}
— end example ]

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/




More information about the cfe-commits mailing list