Macro function with several lines for the parameter?

后端 未结 6 1030
悲哀的现实
悲哀的现实 2021-01-07 23:09

In C++, I need to defined a macro. That macro would take as parameter a \"block\" of code.

Can we safely use several lines of code as parameter of a macro fu

6条回答
  •  感动是毒
    2021-01-08 00:14

    the way to make it work (at least for gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)), is to use braces { } enclosing your actuals for the macro.

    A useful example:

    #ifdef DEBUG
    #define MYDEBUG(X) (X)
    #else
    #define MYDEBUG(X)
    #endif
    
    MYDEBUG({
      if (shit_happens) {
         cerr << "help!" << endl;
         ....
      }
    });
    

提交回复
热议问题