The first snippet below compiles, but the second doesn't. Why?
Asked Answered
P

3

47

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 Ain global scope.

Purely answered 4/1, 2017 at 19:5 Comment(6)
I'd like to understand why @RyanHaining removed it in the first place. Maybe he had a reason.Harri
There's some gray area of course. The general trend I've seen in C++ language-lawyer questions has been towards compiler's disagreeing, or programs compiling without assurance of them being well-defined behavior. These would be places for the "language lawyers" to debate what which compiler was behaving correctly, or whether a program meets the standards requirements for being well defined. Since you should be receiving an accurate error message from this code, IMO it falls outside what is generally posted with [language-lawyer]Laureate
Actually, this doesn't seem a good candidate for the language-lawyer tag. The standard is clear and the snippet is ill-formed. That's all. My two cents.Otorhinolaryngology
@RyanHaining AFAIK language-lawyer simply states that the OP wants a precise reference from the official standard that clarifies what happens in the question. Hence, question about any behaviour can be asked with said tag, whether you find them interesting or not.Jeffiejeffrey
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
@Otorhinolaryngology "this doesn't seem a good candidate for the language-lawyer tag. The standard is clear and the snippet is ill-formed" language-lawyer has nothing to do with the std being unclear!Donelson
R
67

Each of the examples contains declarations of two different classes, both with the name A.

Let's distinguish between the classes by renaming one of them to B:

struct A{ int i = 10; };

int main() {
    struct B{ int i = 20; };
    struct B;
    struct B b;
}

The above is semantically identical to your first example. The class A is never used.

struct A{ int i = 10; };

int main() {
    struct B;
    struct B b;
}

This is semantically identical to your second example. You are trying to create an object of an incomplete type, the forward-declared class B.

Renaming B back to A doesn't change anything because then the declaration of A in main shadows the declaration of the other A at global scope.

[basic.lookup.elab]/2

If the elaborated-type-specifier has no nested-name-specifier, and [...] if the elaborated-type-specifier appears in a declaration with the form:

class-key attribute-specifier-seqopt identifier ;

the elaborated-type-specifier is a declaration that introduces the class-name as described in [basic.scope.pdecl].

So struct A; is a declaration that introduces the class name in the scope of the declaration. Under no circumstances can it refer to a class declared in an outer scope.

[basic.scope.pdecl]/7

[ Note: Other forms of elaborated-type-specifier do not declare a new name [...] — end note ]

By implication, this form of elaborated-type-specifier declares a new name.

Rumpf answered 4/1, 2017 at 20:45 Comment(5)
1) [basic.scope.pdecl]/7 starts with the following statement: The point of declaration of a class **first** declared in an elaborated-type-specifier is as follows:. That means, that bullet points (7.1) and (7.2) are considered only if the declaration class A; is first declared in an elaborated-type-specifier, which is not the case in both examples, as I pointed out at the end of my question.Yeaton
2) So, as far as [basic.scope.pdecl]/7 is concerned, I don't think we can say that the declaration struct A; in main(), in my second example, cannot refer to a class declared in outer scope, as you asserted above. Thanks for your reply anyway.Yeaton
I explained why, in your second example, struct A; is the first declaration of A, because the A at global scope is a completely different class with the same name.Rumpf
[basic.scope.declarative]/1: "The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner declarative region is excluded from the scope of the declaration in the outer declarative region."Rumpf
I think I found the answer to my question in [class.name]/2 which states exactly what you said in your answer (Under no circumstances can it refer to a class declared in an outer scope.). Thanks (+1).Yeaton
R
44

In the second example the line struct A; is a forward declaration for a struct called A in the main function's scope. This struct will be preferred to the global struct A. The next line defines a variable called a of type struct A. Since a struct A was declared in the main function's scope, that's where the compiler will search for it's definition there. It fails to find one (it's commented out). The first example compiles because there is definition in the same scope. The following example will compile however because it specified that A is in the global namespace :

struct A{ int i = 10; };

int main() {
//    struct A{ int i = 20; };
    struct A;
    struct ::A a;
}
Rosalinarosalind answered 4/1, 2017 at 19:8 Comment(1)
@FrançoisAndrieux: I wouldn't characterize it as an "oversight". Your original version was technically correct (struct A; is a declaration, but not a definition). However, writing "forward declaration" rather than "declaration" does make it the answer easier to read for non language lawyers.Dusen
P
5

It doesn't compile because it can't find a definition for A.

int main() {
//    struct A{ int i = 20; };
      struct A;
      struct A a;
}

The code above is equal to your first example, as the global A is shadowed by the local A. In the second example A doesn't have a definition. It's just a prototype. Prototypes are supposed to be placed before a piece of code that needs a definition when the definition is placed AFTER the code which needs it. If the compliler cannot find that definition it will fail because it doesn't know what A is supposed to be (the global definition is shadowed by the local prototype, which causes it to be ignored).

Pettifogger answered 5/1, 2017 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.