7

Possible Duplicate:
C++ alternative tokens?

I'm working on an assignment in C++ with a friend of mine, and we've been doing lots of the coding on his computer/environment (a Macbook Pro with Eclipse). In some of the code he has written conditions using and and or rather than && and ||. The code compiles just fine on his computer, but when I try and compile it on my computer at home (a PC with Visual Studio 2010) I get compiler errors and am forced to switch them. My friend also attests that this syntax has worked using emacs/g++ in a Linux environment.

I had never seen this type of thing before. Is this used widely or supported by most compilers?

Community
  • 1
  • 1
Nick Van Hoogenstyn
  • 1,287
  • 2
  • 13
  • 18
  • 1
    See also: http://msdn.microsoft.com/en-us/library/34h23df8.aspx under "ISO646.H Not Enabled" – Gabe May 15 '11 at 04:53

5 Answers5

10

There are a handful of "alternative representations": and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, and xor_eq. They are a standard language feature of C++.

Visual C++ only supports these as keywords if you compile with the /Za (standards conformance mode) flag.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
6

#include <iso646.h>

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    Being C++, shouldn't this be ``? And isn't that file supposed to be empty? – R. Martinho Fernandes May 15 '11 at 04:49
  • 1
    No, being C++ you need not include anything. These are keywords. Blame your compiler if it doesn't work. – wilhelmtell May 15 '11 at 04:50
  • @Martinho: `ciso646` is just an alias for `iso646.h`, since there aren't any names to move into the std namespace. And either one is a no-op in C++ mode with any conforming compiler. Since adding `#include ` will not harm any conformation compiler, and will make Visual C++ work, so why not? – Ben Voigt May 15 '11 at 04:54
3

They're called operator synonyms, and they're apparently part of the standard, but I'm not sure how widely supported they are.

hammar
  • 138,522
  • 17
  • 304
  • 385
1

Could you add defines to the top to get around it?

#define and &&
#define or ||
zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • 2
    Strictly speaking, no, since `and` and `or` are keywords and cannot be defined as macros. – James McNellis May 15 '11 at 04:45
  • 1
    @JamesMcNellis: Strictly speaking they are alternative tokens, not keywords. You still can do this because technically `#define and &&` is just another way to spell `#define && &&` which is not legal. – CB Bailey May 15 '11 at 11:05
  • @Charles: Oh. Well, it is very misleading that they are listed in the subclause named "Keywords." You're right, of course. – James McNellis May 15 '11 at 20:20
-1

It is either caused by a compiler extension or a #define somewhere else in the source code, possibly from iso646.h defineing or to be || and and to be &&. You might also see something like using not in place of ! as well.

If it is a compiler extension and you want to avoid having to go through and change all the or and and to || and &&, simply add some #defines of your own at the top:

#define or ||
#define and &&
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Would it make more sense then to always just use the standard version? Seems like that is the smartest course of action. – Nick Van Hoogenstyn May 15 '11 at 04:44
  • No, `and` / `or` / `eq` / `bitand` / etc. are keywords in C++. They're synonyms for the operators. – Chris Lutz May 15 '11 at 04:45
  • @Chris can you really count something that has to be `define`d as a keyword? – Seth Carnegie May 15 '11 at 04:48
  • 1
    @Seth - They don't have to be `#define`d. They're keywords. Visual C++ doesn't follow the standards. – Chris Lutz May 15 '11 at 04:50
  • @Seth: According to the C++ standard, they are reserved words, and cannot be defined as macros. In Visual C++, like C, they're enabled by `iso646.h`. But if Visual C++ uses macros to implement them, that's an implementation detail. It doesn't make it legal for the programmer to `#undef` or redefine them (unfortunately, since I once wanted to `#undef` them). – Ben Voigt May 15 '11 at 04:51