1

I read C++ provides additional operators to the usual &,|, and ! which are "and","or" and "not" respectively, plus they come with automatic short circuiting properties where applicable.

I would like to use these operators in my code but the compiler interprets them as identifiers and throws an error.

I am using Visual C++ 2008 Express Edition with SP1. How do I activate these operators to use in my code?

2 Answers 2

4

If you want to have the 'and', 'or', 'xor', etc keyword versions of the operators made available in MSVC++ then you either have to use the '/Za' option to disable extensions or you have to include iso646.h.

Sign up to request clarification or add additional context in comments.

8 Comments

Having made your day, I'd strongly suggest avoiding using the 'alternative tokens' for the operators. I think that most C++ programmers reading the code would prefer to see the usual operators.
Well then, I think it is time for a change. We are supposed to get more descriptive with our variable names and the like right (i bet you'd scream if you saw int i = 3.142;). the token "and" and more descriptive in my opinion than & or &&
Personally, I don't think it helps understanding to use the words instead of the symbols. You'll still have to get a handle on the old style to read code other people write. And adding the choice to C++ doesn't add it to C, Java, C#, Objective-C, or JavaScript. And either way, you're going to have to get "bitwise" and "logical" straight or fall into some really hard-to-debug situations.
I understand that it's a matter of opinion or style, but I think that people are used to the punctuation-style tokens and would find the keyword variants something they have to 'stop and think about'. I'm currently working a project where the existing code mostly uses macros for the logical operators (AND, OR, NEQ, etc), and I must say I don't like it at all... I believe the alternative keyword tokens were provided mostly to aid programmers with non-US keyboards that didn't always have the correct punctuation characters, not an attempt to make them more readable (just more type-able).
A benefit to using the symbol style is that it is easy to distinguish between operators and variables when scanning code. It's a lot harder when everything is alphanumeric text.
|
2

The traditional C++ spelling [*] (just like in C) is && for "logical", short-circuit and, || for "logical", short-circuit or. ! is "logical" not (of course it doesn't short-circuit: what ever would that mean?!-). The bitwise versions are &, |, ~.

According to the C++ standard, the spellings you like (and, or, and so on) should also be implemented, but apparently popular compilers disobey that rule. However you should be able to #include <ciso646> or #include <iso646.h> to hack around that via macros -- see this page, and if your favorite compiler is missing these header files, just create them the obvious way, i.e.,

#define and &&
#define or ||

and so on. (Kudos and gratitude to the commenters for making me research the issue better and find this out!)

8 Comments

Recent versions of the C++ standard include spellings such as "and" and "or".
@Greg good point, added a footnote on the likely future standard (albeit of, more or less, just "academic" interest at this point in time;-).
'and', 'bitor', 'or', 'xor', 'compl', 'bitand', 'and_eq', 'or_eq', 'xor_eq', 'not' and 'not_eq' are all valid alternative tokens for '&&', '|', '||', '^', '~', '&', '&=', '|=', '^=', '!' and '!=' respectively in the current standard (2003).
VC 2008 is not stricly compliant, see here: stackoverflow.com/questions/198402/…
The and, or, bitand etc. keywords are defined in the original 1998 C++ Standard - see section 2.4.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.