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 .
Shafik Yaghmour May 22 '15 at 19:20 2015-05-22 19:20
source share