tags:

views:

71

answers:

1

The C++ standard (ISO/IEC 14882:03) states the following (2.11/2):

Furthermore, the alternative representations shown in Table 4 for certain operators and punctuators (2.5) are reserved and shall not be used otherwise:

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq

I have tried using these with multiple compilers without problems.

From my understanding I'm only forbidden to use these as identifier names. English isn't my first language, so it would be nice if someone could verify that I can indeed use these bit-wise mnemonics in expressions (i.e., "int i = not 0;").

EDIT: I should probably point out that I'm not going to obscure code by using these (if they are valid). It's just for general knowledge. =)

Thanks!

+2  A: 

Yes, you can use them as alternative to name tokens. For example:

struct foo {
    // defines a destructor
    compl foo() { }
};

Your example would work too. It would however store an one into i. If you want to use bitwise not, you use compl (~):

int i = compl 0;
Johannes Schaub - litb
Excellent, thanks!
GlobalKiller
You can't use compl like that. You are relying on an implementation detail of your compiler. They are reserved for use as alternatives operators. It just happens your compiler probably uses a macro to do the dirty work. Another compiler may explicitly use the token.
Martin York
@Martin - Could you clarify? The standard says: "In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling." I've never used these (and MSVC appears not to support them), but I'm still curious.
Michael Burr
Interesting. VC 9 (/Za), a long with a handful of other compilers eat this snippet without trouble.
GlobalKiller
Martin, they are called alternative tokens. by all i know, the use as i showed it is all fine. the dtor trick has come up several times in the c++ irc channel at freenode, so that was the first i had in mind :)
Johannes Schaub - litb
@GlobalKiller - you're right with MSVC disabling extensions (/Za) enables support for these, as does adding iso646.h (which I guess just makes them macros). I suspect that MS found that having these as alternative tokens by default broke some code.
Michael Burr
Retracting original comment. Reading the standard. 2.5 Alternative tokens. It looks like you can use it like this. Not that I would recommend it. These alternatives are designed for use in expressions.
Martin York