Why do C ++ developers support keyword reuse?

What is the main argument for reusing short keywords (and adding context-sensitive values) instead of adding more keywords?

Is it just that you want to avoid breaking existing code that might already use the new keyword, or is there a deeper reason?

The new "enum class" in C ++ 11 made me think about it, but this is a common language design issue.

+63
c ++ keyword c ++ 11 language-design
Sep 24 '15 at 9:12
source share
5 answers

Is it just that you want to avoid breaking existing code that might already use the new keyword, or is there a deeper reason?

No, that’s the reason.

Keywords by definition are always considered keywords wherever they are in the source, so they cannot be used for other purposes. Creating a keyword breaks up any code that can use this token as the name of a variable, function, or type.

Committee C takes a different approach and adds new keywords using _Reserved names, for example. _Atomic , _Bool , and then they add a new header ( <stdatomic.h> , <stdbool.h> ) with a nicer macro, so you can choose whether to include the header to get the name atomic or bool , but it will not be automatically declared and will not violate code that is already used with these names.

The C ++ Committee does not like macros and wants them to be the right keywords, so either reuse existing ones (like auto ), or add context-sensitive "keywords" (which are not really keywords, but are " identifiers with a special meaning ", so they can be used for other things, such as override ), or use weird letters that are unlikely to decltype with user code (for example, decltype instead of the widely supported typeof extension).

+68
Sep 24 '15 at 9:17
source share

Some older languages ​​had no keywords at all, in particular PL / 1 , where

 IF IF=THEN THEN BEGIN; /* some more code */ END; 

was the legal part of the code, but completely unreadable. (Look also at the APL as an example of a write-based programming language that is completely cryptic to read a few months later even by the author of the original code).

The C and C ++ language family has a set of keywords defined by the language specification. But there are very widely used languages ​​with billions of obsolete lines of source code. If you (or their standardization committee) add a new keyword, there is a chance of a collision with some existing program, and, you guessed it, while others answered, this is bad. Therefore, if a standard was added, for example, enum_class as a new keyword, most likely someone would already use it as an identifier, and this object would be unhappy (to change the code when adopting the new C ++ standard).

It is also known that C ++ is parsed slowly (in particular, since standard headers such as <vector> stretch tens of thousands of lines of source code, and because the modules are not yet in C ++ and because the syntax is very ambiguous), therefore the analyzer’s complexity for processing new syntax is not a big problem (C ++ parsing has always been terrible). For example, the GCC community is much more complicated with respect to new optimizations than for new C ++ features (apparently, the latest features of the C ++ standard library require more work than parsing the new syntax), even if switching from C ++ 03 to C ++ 11 was a huge leap and required a lot of work in the C ++ interface. This is less true for jumping from C ++ 11 to C ++ 14.

Some other languages ​​(for example, some Lisp dialects, such as General Lisp and some Scheme , where you can override the let or if macro and macros in homoiconic languages ​​like these, are very different, because they work on AST , from an unreasonable text replacement in C or C ++ ...) allow you to override existing keywords; read also about hygiene macros . But this can make it difficult to understand the source code a few months later.

+17
Sep 24 '15 at 9:59
source share

I think this is mainly due to the fact that adding keywords will violate the existing code, which, as you suggest, uses this keyword in other contexts.

+10
Sep 24 '15 at 9:17
source share

Is it just that you want to avoid breaking existing code that might already use the new keyword, or is there a deeper reason?

By definition, a keyword is a special token that cannot be used anywhere; as a result, entering the keyword breaks any code that was used to use the identifier with the given spelling.

Some languages ​​use the term contextual keyword to mean a spelling that is interpreted only as a keyword in specific contexts. If a wild identifier has not been used in this context before, it is guaranteed that the introduction of a contextual keyword does not violate the existing code. For example, since no identifier can appear immediately after closing the parenthese in the function signature, this is the place where you can enter the so-called contextual keywords (for example, override or final ).

On the other hand, in places where an identifier was previously allowed, adding a keyword poses a risk. For example:

  • struct H { my_type f; enum { g }; }; : use of the enum class , and not the new keyword, because any new word can be mistaken for starting a data member declaration in this context; only the keyword is unique (in LL (1)), and the introduction of a new one can break the code.
  • void h() { my_type f; auto x = g(); } void h() { my_type f; auto x = g(); } : the use of auto instead of a new keyword is due to the fact that any new word may collide with an existing type. This is an amazing choice because it was already a keyword that could be used at this position in C (by default for type int ), but its value was changed (the excuse was its low probability of using it).

As already mentioned, languages ​​can be developed without keywords in their entirety (Haskell comes close enough) or made in such a way that you can easily enter keywords (for example, if each ad starts with a keyword already and then introduces a new keyword it may not come across) . This happens, only C and C ++, where it is not done, and indeed many C-type languages.

+10
Sep 24 '15 at 12:06
source share

The erroneous enthusiasm "less is more." It is believed (incorrectly) that using fewer keywords, programmers will need to learn less and may be more productive sooner. But this only creates confusion in the syntax.

"Real Perl programmers prefer to visually distinguish." ---- Larry Wall

In other words, use the keyword for only one task.

-eleven
Sep 24 '15 at 13:54 on
source share



All Articles