Can't use “not”, “or”, or “plus” as identifier?

依然范特西╮ 提交于 2019-12-08 15:16:53

问题


I tried to compile this:

enum class conditional_operator { plus, or, not };

But apparently GCC (4.6) thinks these are special, while I can't find a standard that says they are (neither C++0x n3290 or C99 n2794). I'm compiling with g++ -pedantic -std=c++0x. Is this a compiler convenience? How do I turn it off? Shouldn't -std=c++0x turn this "feature" off?

PS: Hmmm, apparently, MarkDown code formatting thinks so too...


回答1:


Look at 2.5. They are alternative tokens for || and !.

There is a bunch of other alternative tokens BTW.

Edit: The rationale for their inclusion is the same as the one of trigraphs: allow the use of non ASCII character sets. The committee has tried to get rid of them (at least of trigraphs, I don't remember for alternative tokens), and has met opposition of people (mostly IBM mainframe users) which are using them.

Edit for completeness: as other have make the remarks, plus isn't in that class and should not be a problem unless you are using namespace std.




回答2:


These are actually defined as alternative tokens (and reserved) oddly enough, as alternative representations for operators. I believe this was originally to aid people who were using keyboards which made the relevant symbols hard to produce, although this seems a pretty poor reason to add extra keywords to the language :(

There may be a GCC compiler option to disable them, but I'm not sure.

(As mentioned in comments, plus should be okay unless you're using the std namespace.)




回答3:


or and not are alternative representations of || and ! respectively. You can't turn them off and you can't use these tokens for anything else, they are part of the language (current C++, not even just C++0x). ( See ISO/IEC 14882:2003 2.5 [lex.digraph] and 2.11 [lex.key] / 2. )

You should be safe with plus unless you use using namespace std; or using std::plus;.




回答4:


The Standard lists keywords in 2.11. There's also a list of alternative representations separate from the keyword list that is reserved and can't be used otherwise, but aren't keywords. and and or are on that list. Section 17.4.3 describes restrictions on programs that use libraries, and 17.4.3.1.3 describes that names declared with external linkage in a header are reserved both in std:: and the global namespace.

In other words, you don't have to go to C++0x to have those problems. and and or are already reserved, and header <functional> contains plus as a templated struct type, and plus is therefore off-limits if <functional> is directly or indirectly #included.

I'm not sure dumping that much stuff into the global namespace was really wise, but that's what the standard says.




回答5:


It is an year 1995 amendment to the C90 standard. Probably a compiler may choose on how to behave on this. GCC probably includes the header as part of the standard library. With microsoft it doesn't and you have to include the iso646.h.

Here is a link to wikipedia regarding this.



来源:https://stackoverflow.com/questions/6253351/cant-use-not-or-or-plus-as-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!