[LLVMdev] MS C++ gives error C2371 on this code while (obviously)gcc compiles it fine

Argiris Kirtzidis akyrtzi at gmail.com
Thu Oct 2 12:32:30 PDT 2008


Jay Freeman (saurik) wrote:
> Those rules only apply to if and switch statements. (Yes, this is insane, 
> but true.) The entire section you are quoting from, 6.4, is titled 
> "Selection statements [stmt.select]", which specifically covers these two 
> cases. A for is an iteration statement, not a selection statement.
>   

See 6.4p2: "The rules for conditions apply both to selection-statements 
and to the for and while statements"

> So, if you read 6.5.3p1 (which is actually about for statements) it states 
> that a for loop is rewritten as a while loop where names defined by the 
> for-init-statement are declared in the declaration space of the condition of 
> the while statement. Then, if you read 6.5.1p2 it states:
>
> <quote>
> -2- When the condition of a while statement is a declaration, the scope of 
> the variable that is declared extends from its point of declaration 
> (basic.scope.pdecl) to the end of the while statement. A while statement of 
> the form
>
> while (T t = x) statement
>
> is equivalent to
> label:
> {                               //  start of condition scope
>     T t = x;
>     if (t) {
> 	statement
> 	goto label;
>     }
> }                               //  end of condition scope
> </quote>
>
> Unlike the case of the for statement, there are no subsequent rules that 
> redefine where this declaration is scoped. This rewrite means that the rules 
> you cite in 6.4 simply do not apply: for loops don't cause conditions in 
> selection statements, and therefore should not be scoped in that manner. 
> They almost are, but the rules for while statements actually force the 
> declaration outside of the condition of the if statement.
>   

Again, see the 6.4p2 reference. 'while' statements should also follow 
this rule:

> 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.

gcc correctly scopes the declaration inside the while statement:

while (int x=0) {
   int x=0;   // error: redeclaration.
}


-Argiris

> You are correct, though, that gcc should be emitting an error in the case of 
> the if statement. Naughty gcc.
>
> -J
>   



More information about the llvm-dev mailing list