Search code examples
c++compiler-constructionoverridingvirtualc++11

Compiler requirement for override and final


I can remember that during the discussion about general attributes which finally lead to the new contextual keywords override and final it was suggested that compiler support for these ore some may be optional (I guess it would read in the standard text as "behavior is implementation-specific). But I can not find any trace about this optionality in the FDIS and the corrections afterwards.

But since not finding it is not proof, I have to ask: Is the support as described in 2.11p2, 9.2 and 10.3 of the FDIS for override and final obligatory for a conforming compiler?

Is it for example required that a conforming compiler rejects

class Foo {
    void func() override; // Error: not virtual, 9.2p9
};

Or is it still conforming by ignoring override?


Solution

  • Yes, it's required that override is not ignored by a conforming implementation. First, override can only appear in the declaration of a virtual member function.

    9.2/9:

    [...] A virt-specifier-seq shall appear only in the declaration of a virtual member function.

    Second, a virtual function which doesn't override a member function of a base class but is marked override makes the program ill-formed.

    10.3/7:

    If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed.

    As both are diagnosable rules of the standard it is illegal for a conforming compiler to ignore violations. (1.4/1)

    The same reasoning applies to final and the relevant requirements are in 9 [class]/3 and 10.3 [class.virtual]/4.