3

I suppose you can add code which will get executed only if 2 terms are simultaneously declared this way:

#ifdef X
#ifdef Y

//code to execute

#endif
#endif

I wonder if there's a way to execute the code if at least X or Y is declared (also consider xor), something like:

#ifdef X or #ifdef Y

// code

#endif

?

kaspersky
  • 3,959
  • 4
  • 33
  • 50

4 Answers4

18

Using defined:

#if defined(X) || defined(Y)
Eli Iser
  • 2,788
  • 1
  • 19
  • 29
  • 2
    So I observed I can even use ^ for an exclusive or. – kaspersky Jul 16 '13 at 11:22
  • Interesting, didn't know that, thanks for the find. – Eli Iser Jul 16 '13 at 11:23
  • @gg.kaspersky That's probably because `defined(X)` will expand to an integer 1 or 0, so technically any operator that works with integer types will work here. You can even do `#if defined(X) + defined(Y)` to get an "or" effect. – Rapptz Jul 16 '13 at 11:26
3

You can use operator|| like this:

#if defined(X) || defined(Y)
Rapptz
  • 20,807
  • 5
  • 72
  • 86
1

You can do this with:

#if defined(X) || defined(Y)
...
#endif
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

You need to say:

#if defined(X) || defined(Y)

The following 2 forms are equivalent:

#ifdef identifier
#if defined identifier
devnull
  • 118,548
  • 33
  • 236
  • 227