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 ^=
Even if valid, that’s so awful methinks, besides: which operation do you mean? bitwise? logical?
Comment by Ignacio — 16/9/2006 @ 21:25
These are standard in C++. If Visual C++ doesn’t accept them, it’s broken. “and” means &&, while & would be “bitand”.
Comment by Anonymous — 19/9/2006 @ 13:54
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.
Comment by Duke Aaron D'Attention — 1/11/2006 @ 6:43