Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the C++ language designers keep re-using keywords?

What is the main argument in favor of re-using short keywords (and adding context-dependent meanings) instead of just adding more keywords?

Is it just that you want to avoid breaking existing code that may already be using a proposed new keyword, or is there a deeper reason?

The new "enum class" in C++11 got me thinking about this, but this is a general language design question.

like image 538
Andrew Wagner Avatar asked Sep 24 '15 09:09

Andrew Wagner


People also ask

Why We Use keywords in C programming?

Keywords are predefined, reserved words in C language and each of which is associated with specific features. These words help us to use the functionality of C language. They have special meaning to the compilers. There are total 32 keywords in C.

What are the keywords in C what restrictions apply to their use?

Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier.

Can you change keywords in C?

You cannot create keywords in a language you did not create. Also semantically making an int to an int * makes no sense. Doesn't typedef simply create synonyms? I mean int* is predefined, and any struct you would use typedef would have to be defined by user ?


1 Answers

Is it just that you want to avoid breaking existing code that may already be using a proposed new keyword, or is there a deeper reason?

No, that's the reason.

Keywords, by definition, are always considered keywords wherever they occur in the source, so they cannot be used for other purposes. Making something a keyword breaks any code that might be using that token as a variable, function, or type name.

The C committee take a different approach and add new keywords using _Reserved names, e.g. _Atomic, _Bool, and then they add a new header (<stdatomic.h>, <stdbool.h>) with a nicer macro, so that you can choose whether to include the header to get the name atomic or bool, but it won't be declared automatically and won't break code that happens to be using those names already.

The C++ committee don't like macros and want them to be proper keywords, so either re-use existing ones (such as auto) or add context-dependent "keywords" (which are not really keywords, but are "identifiers with special meaning" so they can be used for other things, such as override) or use strange spellings that are unlikely to clash with user code (such as decltype instead of the widely supported typeof extension).

like image 123
Jonathan Wakely Avatar answered Oct 18 '22 01:10

Jonathan Wakely