Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 330
towi Avatar asked Dec 22 '22 08:12

towi


2 Answers

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.

like image 142
CB Bailey Avatar answered Dec 24 '22 02:12

CB Bailey


The use of override and final is optional for the programmer, but nowhere does it say that the compiler can ignore them.

This might have been different for the earlier proposals which used attributes instead of keywords. Attributes leave a lot more freedom to the compiler.

like image 45
Bo Persson Avatar answered Dec 24 '22 00:12

Bo Persson