Why are overridden and trailing identifiers with a special meaning used instead of reserved keywords?

Both parameters were added to override the qualifier and the final qualifier in C ++ 11. They are different from other specifiers added in C ++ 11, such as constexpr and decltype , since they are not keywords and therefore are available for use as identifiers:

int main() { int override = 0 ; // Ok int final = 0 ; // Ok //int constexpr = 0 ; // Error } 

They are called identifiers with a special meaning, which are described in the standard section of the C ++ 11 2.11 project [lex.name] (emphasis added):

The identifiers in table 3 are of particular importance when they appear in a specific context. When referred to in grammar, these identifiers are used explicitly, and do not use the grammar production identifier. any ambiguity as to whether a given identifier has a special meaning is allowed to interpret the token as a regular Identifier.

and Table 3 - Identifiers with lists of special values, both overriding and final.

Why did these two qualifiers turn out to be identifiers with a special meaning instead of keywords?

+29
c ++ keyword c ++ 11
May 22 '15 at 19:20
source share
1 answer

Adding new keywords is difficult because it robs the user of identifiers. As a result, it becomes a compromise between choosing identifiers that can potentially destroy the old code at present using an identifier or choosing names that are unlikely to break the old code, but are ugly or have no sense in how they are used.

In this particular case, redefinition and final ending are used in the grammar in those places where user identifiers cannot appear. Therefore, identifiers can have special meaning in these places, and outside these contexts they can be considered as a regular identifier, leaving identifiers available to users. C++/CLI used this technique since 2005 , and they are called context sensitive keywords, which are described in the C ++ / CLI <> standard . Identifiers.

We can see in the entry, which discusses the trade-offs of various methods of adding support for virtual control attributes in N3163: redefining control using contextual keywords . It discussed three options:

  • The use of [[attributes]] , which was considered undesirable for reasons, including just masking keywords (a modified example from the article below):

     class A : public B { virtual void f [[override]] () { ... } virtual void h [[final]] () { ... } }; 
  • Use reserved keywords that could potentially violate existing code if ugly names are not selected (modified example from the article below):

     class A : public B { virtual void f override_func () { ... } virtual void h final_func () { ... } }; 
  • Use context-sensitive keywords that do not violate the existing code, allow beautiful names (a modified example from the paper below):

     class A : public B { virtual void f() override { ... } virtual void h() final { ... } }; 

The following paragraph from the article summarizes the argument for using context-sensitive keywords in two other ways (emphasis mine):

Rapperswil has observed that this approach can make error correction and syntax highlighting more difficult. For example, syntax highlighting is a bit more complicated, because instead of global highlighting the keyword that you need for parsing to find out, the identifier is in the place where it has special meaning and should be highlighted. But this is not exceptionally difficult, especially not in comparison with other much more complex things that we already have in C ++ in comparison with other languages.

These are minor inconveniences for several authors of the compiler during the week, but for users. This is the right compromise . Otherwise, with ugly globally reserved names (Variant 2) or inappropriate and visible bolt - by attributes (Variant 1) simplifies the work of several authors of the compiler within a week, but it can be millions of users forever .

Changes were applied to the standard through N3206: Override Management: Removing Attributes; and N3272: Subsequent Management Override Actions .

+35
May 22 '15 at 19:20
source share



All Articles