The snippet below compiles (demo):
struct A{ int i = 10; };
int main() {
struct A{ int i = 20; };
struct A;
struct A a;
}
But this doesn't:
struct A{ int i = 10; };
int main() {
// struct A{ int i = 20; };
struct A;
struct A a;
}
I can see that the answer is probably given by these paragraphs in the Standard:
[basic.lookup.elab]/2 and [basic.scope.pdecl]/7.
But I really don't know how to deduce the different behaviors shown above from these two paragraphs.
Note that in the first example the struct A
is not first declared in the elaborated-type-specifier struct A;
, but in the definition of struct A
in main()
.
In the second example, the struct A
is also not first declared in the elaborated-type-specifier struct A;
, but in the definition of struct A
in global scope.
language-lawyer
is a language agnostic tag, this seems like the kind of question that gets it removed in c++. I'm not disagreeing that it meets the stated requirements of it. If I'm in the minority here, nbd it's back now anyway. – Laureate