tags:

views:

364

answers:

4

Hi,

Here is a very simple C++ application I made with QtCreator :

int main(int argc, char *argv[])
{
    int a = 1;
    int b = 2;

    if (a < 1 or b > 3)
    {
       return 1;
    }
    return 0;
}

To me, this is not valid C++, as the keyword or is not a reserved keyword.

But if I compile and run it, it works find without any warnings ! The exit code is 0 and if I change b = 4, the exit code is 1 !

I'm not including anything to make sure there is no hidden define.

This is really strange to me. Is this something Qt is defining ? I didn't find anything in the documentation regarding that.

+27  A: 

According to Wikipedia:

C++ defines keywords to act as aliases for a number of symbols that function as operators: and (&&), bitand (&), and_eq (&=), or (||), bitor (|), or_eq (|=), xor (^), xor_eq (^=), not (!), not_eq (!=), compl (~).

As MadKeithV points out, these replacements came from C's iso646.h, and were included in ISO C++ as operator keywords. The Wikipedia article for iso646.h says that the reason for these keywords was indeed for international and other non-QWERTY keyboards that might not have had easy access to the symbols.

Thomas Owens
Cool - I have been writing C++ since the early 90's and I had no idea about this. I'm not likely to ever _use_ it, but it's good to know.
Graeme Perrow
I didn't know about it either, until I looked. It seems awkward to use, and I'm sure there are a lot of people out there who write C++ and don't know about it.
Thomas Owens
KeithB
+1. Some guys on IRC did this some day: `struct y { compl y(); };` :)
Johannes Schaub - litb
This was news to me. +1
suszterpatt
I've actually started recently using <ciso646> (the proper way to include iso646.h in a C++ program) in some places, especially unit tests. For example: `if (!someCondition)` looks much better to me as `if (not someCondition)` since sometimes at a glance the ! gets lost next to the parens.
Patrick Johnmeyer
+8  A: 

or is a C++ keyword, and you're allowed to use it instead of ||. There is no magic.

The same goes for and and most other logical operators. It's generally best to stick to the commonly known names though, to avoid confusion like this. If you use or, someone will wonder "why does this compile" ;)

jalf
Old answer, I know, but I just want to be cool and pedantic an point out they are not keywords, but alternative tokens. Boy do I feel cool being picky and annoying. </sarcasm>
GMan
Oh right, I didn't realize that distinction. But yeah, looks like you're right. :)
jalf
A: 

Probably something defines something like that:

#define or ||

Then or and || are equivalent.

EDIT: Only true for C, in C++, or is a reserved keyword which does what you expect it to do (e.g. see here)

Johannes Weiß
+1  A: 

iso646.h defines a number of operator alternatives - it's part of the C++ standard.

MadKeithV
`<iso646.h>` is empty in C++.
avakar
It is not in Visual Studio 2003 through 2010 (http://msdn.microsoft.com/en-us/library/bw6140c5%28VS.80%29.aspx)
MadKeithV
Let me rephrase: the file is empty as far as C++ compiler is concerned. Check the sources, you'll see that the macros are conditional on `__cplusplus` macro not being defined.
avakar
Unless _MSC_EXTENSIONS is defined
MadKeithV
MSVC doesn't implement a lot of stuff added since the 1989 C standard. <iso646.h> was added as part of the 1994 changes, along with various wide-character headers and the like.
Jonathan Leffler