views:

334

answers:

2

I read in a book that 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 for some reason the compiler interprets them as identifiers whenever i use them and throws an error. I'm using Visual C++ 2008 Express Edition with SP1. How do i activate these operators to use in my code. Thank you.

+2  A: 

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!)

Alex Martelli
Recent versions of the C++ standard include spellings such as "and" and "or".
Greg Hewgill
@Greg good point, added a footnote on the likely future standard (albeit of, more or less, just "academic" interest at this point in time;-).
Alex Martelli
@ Alex: My bad, I didn't even notice those.Thanks for the correction.
gogole
Charles Bailey
@Charles: So it it that VC++ does not implement these? Could you show me how to access them please
gogole
VC 2008 is not stricly compliant, see here: http://stackoverflow.com/questions/198402/missing-desired-features-in-visual-c/198671#198671
Charles Bailey
MS just makes me wonder sometimes... geez. Thanks Charles for the information. I guess I'll have to wait till 2010 with my fingers crossed hoping MS finally implement it or change my IDE.
gogole
The and, or, bitand etc. keywords are defined in the original 1998 C++ Standard - see section 2.4.
anon
@Charles, thanks, let me edit the answer accordingly.
Alex Martelli
+4  A: 

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.

Michael Burr
@Micheal:Great, you've made my day. Thanks a lot :D.
gogole
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.
Michael Burr
gogole
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.
Nosredna
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).
Michael Burr
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.
emddudley
@emdduley, I agree with that for C-based languages. However, Ruby and Python use the words and manage to be readable. I think Perl gives you the choice.
Nosredna
I've come across this style a few times and never liked it. Makes it difficult to tell whether the original writer intended logical or bitwise operations.
Dan O
A different color for these tokens would solve all the issues you guys are complaining about. I must admit, using theses tokens with their colors indistinguishable from the actual code makes it a bit difficult to spot them. When they are implemented in VC++ and become keywords the different coloration (from black to blue) should make things easier.
gogole