tags:

views:

136

answers:

2

Possible Duplicate:
or is not valid C++ : why does this code compile ?

Hello.

I've recently encountered unusual C++ code written by someone else:

bool operator != (Point p1, Point p2)
{
    return p1.X != p2.X or p1.Y != p2.Y or p1.Z != p2.Z;
};

as far as I can tell, or isn't declared anywhere, even as macros. There are also few ands in the code. As a result, project doesn't build on VC2008 Express. Person that gave me the code said that author has been using mingw compiler.

The question: is this a non-standard compiler feature (I doubt it), is this a part of newer C++ standard (I haven't been watching C++0x), or is this a programmer's problem (say if the guy moved from pascal, he could have been using and/or instead of &&/|| because of habit, or because he thinks it is more "readable").

+4  A: 

It's actually part of the current C++ Standard (see section 2.5/1), but you may have to do something to enable them in your particular implementation.

Here's a list extracted from the standard, which I may have screwed up in the process:

and && 
and_eq &=
bitor | 
or ||
or_eq |=
xor_eq ^= 
xor ^ 
not !
compl ~ 
not_eq != 
bitand & 
anon
+11  A: 

It’s part of the C++ standard. Unfortunately, Visual C++ doesn’t implement that part, and Microsoft’s answer to a bug report is “won’t fix, since nobody uses this.”

A simple workaround is to include the header <ciso646> which defines these keywords for non-compliant compilers.

(I’m one of the few people who prefer this style, and I’m a bit angry at Microsoft for forcing me to use ASCII soup instead of clean, english words to make my code portable.)

Konrad Rudolph
Accepting because you mentioned workaround.
SigTerm
I also prefer this style, especially for `not` which is so much more readable than `!`.
Greg Hewgill
The problem is that it is so uncommon to see this, that when it does pop up any inexperienced programmer goes WTF and posts a question on stackoverflow. So in my mind it is a bad idea to use these constructs because they make the code harder to maintain (as you have to actually look up what they mean).
Martin York
@Martin Would you rather people used digraphs instead? These names are intended for use by people who's keyboards don't have the required symbols, or at least that was the original intent.
anon
@Martin York: "any inexperienced programmer goes WTF" I wouldn't call "inexperienced" every programmer that "goes WTF" about it - I've been using C++ for a several years, and Haven't ever saw "or" in C++ code until today. The funny thing is that this could be happening because of all the guys that use microsoft compilers.
SigTerm
@Martin: By that same logic you must also not use the conditional operator – I’ve lost count of how many *non*-beginners are tripped up by this. Heck, by the same logic you must avoid RAII, SFINAE and smart pointers. So, no. Just … no.
Konrad Rudolph
@Konrad Rudolph: I see your point but I would stick with my original argument. Maintainability is based on consistency. Unlike your other suggestions there is already a well established and well understood set of symbols to do the job adding another parallel set that does the same thing is inconsistent (and I am glad the MS compiler does not support it (as per Neils comments it does not need to as it provides alternative mechanism in the OS to generate the appropriate characters without having to resort to digraphs)).
Martin York