0

Can I use reserved identifiers in a scoped enumerator; and importantly, why not?

enum struct token { void, int, return, not, if };
Mercer
  • 148
  • 1
  • 10

1 Answers1

5

Why not is VERY difficult to answer but isn't really all that important. We could spend years discussing why some features are in a language and why some aren't. It would be a waste of energy. This is the language definition you have to work with and all languages that I'm familiar with follow the same paradigm. (.NET allows reserved keywords used as identifier names with but only when prefixed with a special symbol(s).) Also, no language is perfect and certainly will not fit every single criteria that any individual programmer might be looking for.

But consider if keywords were allowed in enums and in other situations: The compiler would certainly be much, much more complicated and therefore slower. Also the resulting code would be more likely to confuse the reader and make it less maintainable. C++ gives plenty of rope to hang yourself with already. Why ask for more?

But, if you really want a collection of values that reflect the language's reserved symbols then you might consider using something like std::map with string keys and string values. That might come close to giving you what you want w/o any internal voodoo.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • +1 for mentioning readability. It wouldn't make much sense if keywords(in contrast to operators) are allowed to have more than their "normal" meanings and behave differently based on whichever context. Reserved words are *reserved* and like you already pointed out **have predefined semantic meanings.** – iamOgunyinka Aug 10 '14 at 08:19
  • Sure I understand that it's not within it's paradigm and I obviously don't want to get into a dispute of language favouritism. However I would contest that discovering why not is important; since knowing any technical reasons as to why it is not implemented would aid me in developing an appropriate method of implementing my intended use. That is, if there are any technical restrictions. I also oppose your idea that it would make the source more difficult to read, as it is scoped; I don't see why it should be an issue. – Mercer Aug 10 '14 at 10:46
  • In terms of the compiler, well that's the compiler function. It's not up to the programmer to be concerned with the compiler's methods. If it's difficult, it takes more time. Thanks for your response Sasik. – Mercer Aug 10 '14 at 10:49
  • @UserAbuser: Ah, but what about writing cross-platform code? As soon as you start writing code that will be fed into different compilers on different platforms you will suddenly come to know the innards of a compiler pretty quickly! Platform compatibility alone is enough to aim for as simple language spec as possible. Look at the C language specification for a great example. C++ is also not that complex especially considering that it inherits a large part of its functionality from C. – Paul Sasik Aug 10 '14 at 14:33
  • @UserAbuser: Another thought: There are a lot of "Why not" questions on SO. Got a gold badge for one of my own just yesterday with many of the SO heavyweights chiming in: http://stackoverflow.com/questions/3151722/why-arent-code-regions-allowed-within-method-bodies-in-vb-net Now, my question did actually have an answer in the VB (ugh!) spec but usually you will get "because that's what the spec team decided." – Paul Sasik Aug 10 '14 at 14:48