Search code examples
c++language-lawyerderived-class

Why do we need the last two definitions for the grammar production base-specifier?


In [class.derived.general]/1 we have the following definitions for base-specifier:

base-specifier:
    attribute-specifier-seqopt class-or-decltype
    attribute-specifier-seqopt virtual access-specifieropt class-or-decltype
    attribute-specifier-seqopt access-specifier virtualopt class-or-decltype

Why do we need the second and the third definitions above? Why is the virtual required in the second definition while it's optional in the third?


Solution

  • Why do we need the second and the third definitions above?

    Syntactic sugar. Thanks to this, both class Derived: public virtual Base and class Derived: virtual public Base are valid. Much better than having to look up the order once a year when virtual inheritance is needed.

    Why is the virtual required in the second definition while it's optional in the third?

    Note that second option has obligatory virtual and third option has obligatory access-specifier. Thanks to this, both class Derived: virtual Base and class Derived: public Base are valid. And it makes it possible to have the advantage above, as opposed to defining it as
        attribute-specifier-seqopt virtualopt access-specifieropt class-or-decltype

    Perhaps it could be formulated in a different way, but I can't think of anything better to express those two aspects.