1

Possible Duplicate:
C++ and,or,not,xor keywords

I think the title says it, but to expand a little:

Is there a way to replace the && and || operators in your code with "AND" and "OR". It makes it a little more readable, and since im the only one working on the project, I am curious as to whether this can be done. This way instead of writing

if(is_close_enough(enemy) && attackReady(attack))

I can use

if(is_close_enough(enemy) AND attackReady(attack))

I am using VS2010 as my IDE and C++ as my language.

Thank you for your help

Community
  • 1
  • 1
  • 1
    #define AND && // thats it. http://msdn.microsoft.com/en-us/library/teas0593(v=vs.80).aspx – nikeee Jan 06 '13 at 16:54
  • 13
    my opinion: you should try and learn to read *standard* code that uses &&, instead of adapting it to what you are accustomed to. being able to read code is as important as being able to write code – Andy Prowl Jan 06 '13 at 16:56
  • @AndyProwl That's what I would do. You'll need this anyway. – nikeee Jan 06 '13 at 16:57
  • @AndyProwl The tokens `and`, `or`, etc. are in the C++ standard and have been since the beginning. They are completely standard, and the only reason not to use them are coding conventions of your project *(which would dictate things like indentation as well, which are similarly subjective)*. Or if you want to compile your code with a C compiler, but then pretty much every actual real feature of C++ is out as well. I prefer the words, myself...especially `not`. – HostileFork says dont trust SE Jan 06 '13 at 17:04
  • 2
    @HostileFork: I did not know this and I'm glad I learned something new, but i still stand by the spirit of my comment. plus, "and" may be standard, but "AND" is not. – Andy Prowl Jan 06 '13 at 17:07

2 Answers2

9

The language already defines and and or keywords, along with quite a few others, which are for this purpose.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • but does VS2010 support that? – Andy Prowl Jan 06 '13 at 16:56
  • Only one way to find out. – Puppy Jan 06 '13 at 16:58
  • 3
    you have a point :) but since you are the one who gave the answer, I supposed you checked that, because the question maker specified that he is working on VS2010. so I did that myself and the answer is "no, it does not support it". which makes your answer informative (i am upvoting it, i did not know that) but useless for the question maker – Andy Prowl Jan 06 '13 at 17:05
  • 2
    VS2010 *does* support these keywords, but only if you disable Microsoft extensions. *Not* supporting them is an extension... :-) – Bo Persson Jan 06 '13 at 20:21
  • @BoPersson: that's kind of hilarious, but thanks for pointing it out :-) – Andy Prowl Jan 07 '13 at 00:41
8

You can use the preprocessor, but please don't. It makes your code less readable, not more.

#define AND &&
#define OR  ||

If you want to use a language with different keywords, just use a language with different keywords. This kind of hacking will only end in tears.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469