开发者

Can't use "not", "or", or "plus" as identifier?

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开发者_JAVA技巧? How do I turn it off? Shouldn't -std=c++0x turn this "feature" off?

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


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.


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.)


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;.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜