Question

I'm considering an example from the official spec:

class A * A;

I've read that this line introduces the two names: class A which can be accessed via elaborated-specifier-type only and pointer to A. Is this line fully equivalent to the two following lines?

class A;
A * A;

Is that line just syntax sugar and actually we have two lines as I've specified above? Or how is class A * A; parsed by compiler?

Was it helpful?

Solution

Yes, the inline declaration is just syntactic sugar. You're forward-declaring that name and using it on the same line.

OTHER TIPS

I'm looking for more detailed answer in the working draft N3797 and I've found the following:

§8.3.3:

Thus, a declaration of a particular identifier has the form T D where T is of the form attribute-specifier-seq_*opt* decl-specifier-seq and D is a declarator.

§7.1:

decl-specifier:
    storage-class-specifier
    type-specifier
    function-specifier
    friend
    typedef
    constexpr
decl-specifier-seq:
    decl-specifier attribute-specifier-seq_*opt*
    decl-specifier decl-specifier-seq

§7.1.6:

type-specifier:
    trailing-type-specifier
    class-specifier
    enum-specifier

§8.3.1:

In a declaration T D where D has the form * attribute-specifier-seq_*opt* cv-qualifier-seq_*opt* D1 and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T,” then the type of the identifier of D is “derived-declarator-type-list cv-qualifier-seq pointer to T.”

Now class-specifier contains class keyword inside. Hence class A * A is valid grammar construction, where * A is declarator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top