You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CWG3221 [diff.stat] Add entry for C and C++ defining the scope of for loop identifiers differently #984
Affected subclause: [stmt.for]
Change: Names declared inside of the init-statement of a for loop have the same scope as names declared directly in the statement of the same for loop.
[Example 1:
for(inti=0;;){
inti=1; // valid C, invalid in C++returni;
}
- end example]
Rationale: <to be decided>
Effect on original feature: Change to semantics of well-defined feature.
Difficulty of converting: Syntactic transformation. A second compound statement will allow reusing the name.
How widely used: Seldom.
That's not actually correct: [basic.scope.block] says that each iteration statement introduces a block scope, but the substatement also introduces a block scope. So there are two different scopes, but you're not allowed to declare an i in both of them because we have a special rule prohibiting it.
I don't know the exact rationale, and a vague "catch some bugs" seems unconvincing to me. I've suggested in the past that we abolish all special rules of this kind.
That's not actually correct: [basic.scope.block] says that each iteration statement introduces a block scope, but the substatement also introduces a block scope. So there are two different scopes, but you're not allowed to declare an i in both of them because we have a special rule prohibiting it.
Perhaps a better way to describe it would be:
Affected subclause: [stmt.for]
Change: Names declared inside of the init-statement of a for loop cannot be hidden by the same names declared directly in the statement of the same for loop.
[Example 1:
for(inti=0;;){
inti=1; // valid C, invalid in C++returni;
}
- end example]
Rationale: <to be decided>
Effect on original feature: Change to semantics of well-defined feature.
Difficulty of converting: Syntactic transformation. A second compound statement will allow reusing the name.
How widely used: Seldom.
changed the title [-][diff.stat] Add entry for C and C++ defining the scope of for loop identifiers differently[/-][+]CWG3221 [diff.stat] Add entry for C and C++ defining the scope of for loop identifiers differently[/+]on Aug 23, 2026
Activity
t3nsor commentedon Aug 21, 2026
That's not actually correct: [basic.scope.block] says that each iteration statement introduces a block scope, but the substatement also introduces a block scope. So there are two different scopes, but you're not allowed to declare an
iin both of them because we have a special rule prohibiting it.I don't know the exact rationale, and a vague "catch some bugs" seems unconvincing to me. I've suggested in the past that we abolish all special rules of this kind.
Halalaluyafail3 commentedon Aug 22, 2026
Perhaps a better way to describe it would be:
jensmaurer commentedon Aug 23, 2026
CWG3221
Removing the special-case rule is probably an EWG matter.
[-][diff.stat] Add entry for C and C++ defining the scope of for loop identifiers differently[/-][+]CWG3221 [diff.stat] Add entry for C and C++ defining the scope of for loop identifiers differently[/+]Halalaluyafail3 commentedon Aug 27, 2026
I believe the possessive form should be C++'s instead of C++', at least that is what the note in [basic.pre]/1 uses.