24

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

B Faley
  • 17,120
  • 43
  • 133
  • 223
Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • 15
    What do you mean well spotted? or **is** a keyword in C++. – Michael Foukarakis Sep 17 '09 at 06:02
  • @MichaelFoukarakis I think *`well spotted`* means the *`why does this code compile?`* part of the question: Some compilers need `#include ` and some not. Most (if not all) IDEs don't highlight these new "keywords". – Wolf Jan 10 '17 at 10:35

3 Answers3

55

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.

Community
  • 1
  • 1
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
  • 2
    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 Sep 16 '09 at 14:42
  • 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 Sep 16 '09 at 15:02
  • 1
    I believe that these were added to help people with other-than-English keyboards that may not have all of the symbol keys (&, |, etc). – KeithB Sep 16 '09 at 15:27
  • 3
    +1. Some guys on IRC did this some day: `struct y { compl y(); };` :) – Johannes Schaub - litb Sep 16 '09 at 15:47
  • 3
    I've actually started recently using (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 Sep 17 '09 at 01:08
  • @FelixDombek It is needed if your compiler does not conform to the standard, and many compilers over the years have not. :) I don't remember what compiler I was using in '09 but I did need the include. That said, not sure I'd recommend this practice to others, in hindsight -- especially given the inconsistent naming of the alternate forms. – Patrick Johnmeyer Aug 22 '15 at 03:39
16

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
  • 243,077
  • 51
  • 345
  • 550
  • 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. – GManNickG Jan 24 '10 at 08:18
  • Oh right, I didn't realize that distinction. But yeah, looks like you're right. :) – jalf Jan 24 '10 at 14:37
  • @GManNickG: They are keywords (at least in the current working draft): Section C.5.2.3: "The tokens and, and_eq, bitand, bitor, compl, not_eq, not, or, or_eq, xor, and xor_eq are keywords in this International Standard (2.11). They do not appear as macro names defined in ." – Hannes Apr 22 '16 at 11:57
7

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

Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75