Operators and, or and xor written in English: is this standard C++?

Kamel was reviewing some code I wrote and through a question he asked, I realized that some code I wrote would not compile under Visual C++. Further investigations showed that the following is valid under GCC, but not under Visual C++:


#include
using std::cout;
using std::endl;
int main(int argv, char ** args)
{
int a = 7;
int b = 3;
cout << (a and b) << endl;
cout << (a or b) << endl;
cout << (a xor b) << endl;
return 0
}

Can anyone help us out? Is this correct code?

Update: It looks like you can get this result under Visual C++ by including “iso646.h”. It includes the following definitions:

#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=

3 thoughts on “Operators and, or and xor written in English: is this standard C++?”

  1. These are standard in C++. If Visual C++ doesn’t accept them, it’s broken. “and” means &&, while & would be “bitand”.

  2. You should be able to use the following operators (logical then bitwise)…

    and = &&, &
    or = ||, |
    xor = ^

    …without having to include “iso646.h”. This syntax has always been part of ANSI C right from the outset; it also makes no difference which version of VC++ you are using.

    Just ensure that you start a command-line project, as opposed to a Windows-based project, in VC++. This should include the basic headers for you, and won’t bloat your code.

    Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax