Why for (class A {} fkldsjflksdjflsj ;;) can compile?

I was putting something wrong in the for loop, but it looks like it can still compile. Then I tried the for loop syntax, and it seems like the following code: defining a class inside a run condition in a loop with some meaningless characters like this might compile.

int main(){
    for(class A{} fkldsjflksdjflsj;;)
    return 0;



for(class A;;)
for(class A{};;)


But not.

for(class A fkldsjflksdjflsj;;)




source to share

3 answers

The first part for

can contain a variable declaration. (And some other types of declaration). Fortunately, this is what we have:

class A
} zzz;


declares a variable zzz

whose type class A

is a class with no custom members.

class A fkldsjflksdjflsj

fails because it is class A

not defined. But it would be nice if you defined earlier class A




You have declared an instance of a class A

named fkldsjflksdjflsj

within the scope for-loop. The declaration class A fkldsjflksdjflsj

fails because you didn't specify a class body.




are not "meaningless characters". This is the name of a type variable class A

that you also just declared using the syntax class A {}

. So, all this is a variable declaration, which is what the first part of the statement requires for




All Articles