Why does C ++ 11 redefine final attributes as well?

I somehow missed that the attributes were introduced in C ++ 11. Now I found out, I wonder why override and final were added as identifiers with a special value, and not as standard attributes.

The purpose of override is to generate a compile-time error, and this is also the goal of many standard attributes. It seems like they will fit into this concept, but there is probably a reason why I am missing.

+6
source share
2 answers

They were, once, before they were changed in response to a comment by US 44 on C ++ 11 FCD:

Even if attributes continue to be standardized by continuing objections from both two providers, which are cited as the main prior art, we can live with them, with the exception of virtual override controls. This result is simply terrible, as already shown in the example in 7.6.5 (excerpt):

 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 words [[decorated]] . There has already been public mockery of C ++ 0x about this ugliness. It's just a bad language design, even in the face of backward compatibility (for example, that some existing code can already use these words as identifiers), since these problems have already been solved in other ways in existing practice (see below). More importantly, it is precisely the misuse of attributes as disguised keywords that were opposed and clearly promised that this would not happen, to get this offer passed. Using attributes for virtual control keywords is the most egregious abuse of attribute syntax, and the use of attributes should be fixed by replacing them with non-attribute syntax. These virtual override controls are linguistic and not annotations.

It is possible to have good names and no conflicts with existing code using contextual keywords, such as recognizing that the word has special meaning when it appears in the grammar position, where the user identifier cannot appear, as demonstrated in C ++ / CLI, which has five years of actual field experience with a large number of clients (and certainly not the name of conflicts or problems with the confusion of programmers, these five years were 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 realizable, 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 should be a more natural replacement for [[attributed]] keywords for virtual redefinition management, which is an ugly novelty that has no field experience and what developers have already ridiculed.

+7
source

To just point out http://en.cppreference.com/w/cpp/language/attributes :

Attributes provide a unified standard syntax for implementation-defined language extensions, such as Java extensions and language extensions ((...)), Microsoft extension __declspec (), etc.

It is very clear that standard language keywords are not attributes.

0
source

Source: https://habr.com/ru/post/1014062/


All Articles