Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When did "and" become an operator in C++

Tags:

c++

g++

I have some code that looks like:

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

This causes an error in g++ like so:

Row.cpp:140: error: expected unqualified-id before '&&' token 

so after cursing the fool that defined "and" as &&, I added

#ifdef and #undef and #endif 

and now I get

Row.cpp:9:8: error: "and" cannot be used as a macro name as it is an operator in C++ 

Which leads to my question of WHEN did "and" become an operator in C++? I can't find anything that indicates it is, except of course this message from g++

like image 987
boatcoder Avatar asked Mar 10 '10 19:03

boatcoder


People also ask

What is & operator in C?

The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

What is double and operator in C?

The logical AND operator is represented as the '&&' double ampersand symbol. It checks the condition of two or more operands by combining in an expression, and if all the conditions are true, the logical AND operator returns the Boolean value true or 1. Else it returns false or 0.


2 Answers

From the C++03 standard, section 2.5:

2.5 Alternative tokens

Alternative token representations are provided for some operators and punctuators. In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling. The set of alternative tokens is defined in Table 2.

Table 2—alternative tokens

alternative primary    <%         {    %>         }    <:         [    :>         ]    %:         #    %:%:       ##    and        &&    bitor      |    or         ||    xor        ˆ    compl      ˜    bitand     &    and_eq     &=    or_eq      |=    xor_eq     ˆ=    not        !    not_eq     !=  
like image 136
interjay Avatar answered Oct 14 '22 13:10

interjay


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.

like image 29
Jerry Coffin Avatar answered Oct 14 '22 13:10

Jerry Coffin