Multi-statement Macros in C++

前端 未结 7 1362
轻奢々
轻奢々 2021-01-19 08:00

In C++, is it possible to make a multi-statement macro with nested if statements inside of it like the one below? I\'ve be

相关标签:
7条回答
  • 2021-01-19 09:00

    For a multi-line macro you need to add a \ character to the end of all but the last line to tell the macro processor to continue parsing the macro on the next line, like so:

    #define MATCH_SYMBOL( symbol, token) \
         if(something == symbol){        \
              if( symbol == '-'){        \
              }else if (symbol != '-'){  \
              }                          \
         other steps;                    \
         }
    

    Right now, it's trying to interpret it as a 1-line macro and then some actual code at the top of your file, which isn't what you want:

    #define MATCH_SYMBOL( symbol, token)
    
    // and then... wrongly thinking this is separate...
    
     if(something == symbol){ // symbol was never defined, because the macro was never used here!
          if( symbol == '-'){
          }else if (symbol != '-'){
          }
     other steps;
     }
    
    0 讨论(0)
提交回复
热议问题