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

后端 未结 3 1788
难免孤独
难免孤独 2020-12-03 10:21

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)         


        
相关标签:
3条回答
  • 2020-12-03 10:45

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

    0 讨论(0)
  • 2020-12-03 11:01

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

    0 讨论(0)
  • 2020-12-03 11:03

    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.

    0 讨论(0)
提交回复
热议问题