#include <iostream>
int main(){
int b = 2;
if(int a = 0){ // #condition
}else if(b == 2){
int a; //#1
}
}
The above code can be compiled in both gcc and clang. However, according to what the relevant rules say, this code should be ill-formed. The rules are:
A name introduced by a declaration in a condition (either introduced by the decl-specifier-seq or the declarator of the condition) is in scope from its point of declaration until the end of the substatements controlled by the condition. If the name is redeclared in the outermost block of a substatement controlled by the condition, the declaration that redeclares the name is ill-formed.
Isn't the if-statement which follows the else not a substatement controlled by the condition? (i.e, only the condition at #condition executed to be false will the if-statement after else be executed). So, why a declaration that redeclares the name in the outermost block of such a substatement can be considered as a well-formed code?
Maybe there are some arguments for the phrase "substatement controlled by the condition" in that rules of version n4659, however, such an idea is obviously clear in the latest draft.
A substatement of a statement is one of the following:
for a selection-statement, any of its statements (but not its init-statement)
That means the statement after else is a substatement of the primary if-statement, then the next rules say that:
stmt.stmt#stmt.pre-5
A name introduced in a selection-statement or iteration-statement outside of any substatement is in scope from its point of declaration until the end of the statement's substatements. Such a name cannot be redeclared in the outermost block of any of the substatements
The rule obviously says that we cannot redeclared the same name as that declared in condition in the outermost block of these substatements. So, I'm curious whether I have misunderstood these rules or there are some defectives in the draft?