5

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

Community
  • 1
  • 1
SigTerm
  • 26,089
  • 6
  • 66
  • 115

2 Answers2

16

It’s part of the C++ standard and works on all modern, conforming C++ compilers.

For Visual C++, this means passing the /permissive- flag to the compiler, which is recommended anyway (this flag is set by default by Visual Studio 2017 and later).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    I also prefer this style, especially for `not` which is so much more readable than `!`. – Greg Hewgill Aug 10 '10 at 11:52
  • 4
    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 Aug 10 '10 at 11:54
  • @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. –  Aug 10 '10 at 12:10
  • 2
    @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 Aug 10 '10 at 12:24
  • @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 Aug 10 '10 at 14:06
  • @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 Aug 10 '10 at 15:47
4

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 &