pass block instruction as macro's argument in C

后端 未结 3 923
鱼传尺愫
鱼传尺愫 2021-01-26 18:02

I don\'t know if that is possible, I want to pass a block of instructions in macro like an argument.I will show you an example:

#define ADD_MACRO(size, BLOCK){ f         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 18:36

    The only problem with the given macro is that it doesn't handle commas in the BLOCK. Commas are tolerated by a variadic macro parameter:

    #define ADD_MACRO(size, ...) do { for(int i=0; i

    (Also, common practice is to enclose statement macros in do … while(0) to force the user to include a semicolon.)

    (Of course, the original problem may have a better solution. The preprocessor is a blunt instrument. This is the preprocessor solution to the stated question.)

提交回复
热议问题