When did “and” become an operator in C++

后端 未结 7 1146
后悔当初
后悔当初 2020-12-05 18:00

I have some code that looks like:

static const std::string and(\" AND \");

This causes an error in g++ like so:

Row.cpp:140         


        
相关标签:
7条回答
  • 2020-12-05 18:09

    I don't know when it was introduced, it may well have been there from the beginning, but I believe the reason it's there is as an alternative to && for people with restricted character sets i.e. where they don't actually have the ampersand character.

    There are many others too eg. and_eq, or, compl and not to name just a selection.

    0 讨论(0)
  • 2020-12-05 18:10

    They were added because some of those characters are difficult to type on some keyboards.

    0 讨论(0)
  • 2020-12-05 18:11

    There are several such alternatives defined in C++. You can probably use switches to turn these on/off.

    0 讨论(0)
  • 2020-12-05 18:15

    They've been there since C++ 98. They're listed in the §2.5/2 of the standard (either the 1998 or the 2003 edition). The alternate tokens include: and, or, xor, not, bitand, bitor, compl, and_eq, or_eq, xor_eq, not, not_eq.

    0 讨论(0)
  • 2020-12-05 18:17

    According to C++ Standard 2.12 there are predefined preprocessor tokens "which are used in the syntax of the preprocessor or are converted into tokens for operators and punctuators." and is one of them. In new C++ Standard there is new 2.12/2:

    Furthermore, the alternative representations shown in Table 4 for certain operators and punctuators (2.6) are reserved and shall not be used otherwise:

    and and_eq bitand bitor compl not
    not_eq or or_eq xor xor_eq
    
    0 讨论(0)
  • 2020-12-05 18:19

    You can use -fno-operator-names to disable this. Alternatively, you can name your std::string object something else!

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