michael
michael

Reputation: 110510

How to add a 'or' condition in #ifdef

How can I add a 'or' condition in #ifdef ?

I have tried:

#ifdef CONDITION1 || CONDITION2

#endif

This does not work.

Upvotes: 242

Views: 201328

Answers (4)

terwxqian
terwxqian

Reputation: 659

Check this:

    #if defined __WINDOWS__ && ( _MSC_VER >= 1700 )
        enum class FUTURES_DS_STAGE{
            ...
        };
    #else
        enum FUTURES_DS_STAGE{
        ...
        };
    #endif

Upvotes: 0

KANJICODER
KANJICODER

Reputation: 3885

I am really OCD about maintaining strict column limits, and not a fan of "\" line continuation because you can't put a comment after it, so here is my method.

//|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|//
#ifdef  CONDITION_01             //|       |//
#define             TEMP_MACRO   //|       |//
#endif                           //|       |//
#ifdef  CONDITION_02             //|       |//
#define             TEMP_MACRO   //|       |//
#endif                           //|       |//
#ifdef  CONDITION_03             //|       |//
#define             TEMP_MACRO   //|       |//
#endif                           //|       |//
#ifdef              TEMP_MACRO   //|       |//
//|-  --  --  --  --  --  --  --  --  --  -|//

printf("[IF_CONDITION:(1|2|3)]\n");

//|-  --  --  --  --  --  --  --  --  --  -|//
#endif                           //|       |//
#undef              TEMP_MACRO   //|       |//
//|________________________________________|//

Upvotes: -7

Minhas Kamal
Minhas Kamal

Reputation: 22116

May use this-

#if defined CONDITION1 || defined CONDITION2
//your code here
#endif

This also does the same-

#if defined(CONDITION1) || defined(CONDITION2)
//your code here
#endif

Further-

  • AND: #if defined CONDITION1 && defined CONDITION2
  • XOR: #if defined CONDITION1 ^ defined CONDITION2
  • AND NOT: #if defined CONDITION1 && !defined CONDITION2

Upvotes: 61

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247899

#if defined(CONDITION1) || defined(CONDITION2)

should work. :)

#ifdef is a bit less typing, but doesn't work well with more complex conditions

Upvotes: 443

Related Questions