How to add a 'or' condition in #ifdef

前端 未结 3 1247
情话喂你
情话喂你 2020-12-22 21:15

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

I have tried:

#ifdef CONDITION1 || CONDITION2

#endif
相关标签:
3条回答
  • 2020-12-22 21:54
    #if defined(CONDITION1) || defined(CONDITION2)
    

    should work. :)

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

    0 讨论(0)
  • 2020-12-22 22:07

    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
    0 讨论(0)
  • 2020-12-22 22:18

    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   //|       |//
    //|________________________________________|//
    
    0 讨论(0)
提交回复
热议问题