Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C++11 override and final not attributes?

Tags:

c++

c++11

I somehow missed that attributes were introduced in C++11. Now I found out, I'm wondering why override and final were added as identifiers with special meaning and not as standard attributes.

The purpose of override is to generate a compile time error and this is also the purpose of many of the standard attributes. It feels as if they would fit in that concept but there is probably a reason for it that I'm missing.

like image 466
ygram Avatar asked Jan 13 '17 15:01

ygram


1 Answers

They were, once, before they got changed in response to comment US 44 on C++11's FCD:

Even if attributes continue to be standardized over continued objections from both of the two vendors who are cited as the principal prior art, we can live with them with the exception of the virtual override controls. This result is just awful, as already shown in the example in 7.6.5 (excerpted):

class D [[base_check]] : public B {
    void some_func [[override]] ();
    virtual void h [[hiding]] (char*); 
};

Here we have six keywords (not counting void and char): three normal keywords, and three [[decorated]] keywords. There has already been public ridicule of C++0x about this ugliness. This is just a poor language design, even in the face of backward compatibility concerns (e.g., that some existing code may already use those words as identifiers) because those concerns have already been resolved in other ways in existing practice (see below). More importantly, this is exactly the abuse of attributes as disguised keywords that was objected to and was explicitly promised not to happen in order to get this proposal passed. The use of attributes for the virtual control keywords is the most egregious abuse of the attribute syntax, and at least that use of attributes must be fixed by replacing them with non-attribute syntax. These virtual override controls are language features, not annotations.

It is possible to have nice names and no conflicts with existing code by using contextual keywords, such as recognizing the word as having the special meaning when it appears in a grammar position where no user identifier can appear, as demonstrated in C++/CLI which has five years of actual field experience with a large number of customers (and exactly no name conflict or programmer confusion problems reported in the field during the five years this has been available):

class D : public B {
    void some_func() override; // same meaning as [[override]] - explicit override
    virtual void h (char*) new; // same meaning as [[hiding]] - a new function, not an override
};
int override = 42; // ok, override is not a reserved keyword

The above forms are implementable, have been implemented, have years of practical field experience, and work. Developers love them. Whether the answer is to follow this existing practice or something else, there needs to be a more natural replacement for the currently [[attributed]] keywords for virtual override control which is an ugly novelty that has no field experience and that developers have already ridiculed.

like image 166
T.C. Avatar answered Sep 18 '22 13:09

T.C.