Macro function with several lines for the parameter?

后端 未结 6 906
悲哀的现实
悲哀的现实 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:03

    I think you will need to use extra parentheses to make your expressions look like a single argument which won't get broken up by the preprocessor, i.e. do it more like this:

    #define MY_MACRO( (expr) ) DOSOMETHING( (expr) ); DOANOTHERTHING( (expr) ); // etc...
    
    int my_function() {
        MY_MACRO( 
            (int k = AFunction();
            k++;
            AnotherFunction( k );)
        ); 
    }
    

    although I haven't actually tried it.

提交回复
热议问题