简体   繁体   English

范围列举器中的C ++关键字

[英]C++ keywords in scoped enumerators

Can I use reserved identifiers in a scoped enumerator; 我可以在作用域枚举器中使用保留的标识符吗? and importantly, why not? 重要的是,为什么不呢?

enum struct token { void, int, return, not, if };

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. (.NET允许将保留关键字用作标识符名称,但仅当使用特殊符号作为前缀时才使用。)而且,没有一种语言是完美的,并且肯定不能满足任何单个程序员可能正在寻找的每个标准。

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. C ++可以为您提供很多帮助。 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. 但是,如果您确实想要一个反映语言保留符号的值集合,则可以考虑使用带有字符串键和字符串值的std::map类的东西。 That might come close to giving you what you want w/o any internal voodoo. 这可能几乎给了您想要什么而没有任何内部伏都教。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM