Macro Expansion: Argument with Commas

前端 未结 3 1025
天涯浪人
天涯浪人 2021-01-01 00:29

The code I\'m working on uses some very convoluted macro voodoo in order to generate code, but in the end there is a construct that looks like this

#define A         


        
3条回答
  •  既然无缘
    2021-01-01 01:08

    Curiuosly enough, the following appears to work in MSVC (tested with 2010 and 2015).

    #define ARGS 1,2,3
    
    #define OUTER(...) INNER PARAN(__VA_ARGS__)
    #define PARAN(...) (__VA_ARGS__)
    #define INNER(A,B,C) A + B + C
    
    int a = OUTER(ARGS);
    

    I don't know that it's supposed to work by the letter of the standard, in fact I have a hunch it's not. Could still be conditionally compiled just for MSVC, as a workaround.


    [EDIT]  P.S.  As pointed out in the comments, the above is (another) non-standard MSVC behavior. Instead, the alternative workarounds posted by @rici and @JohnBollinger in the respective replies are compliant, thus recommended.

提交回复
热议问题