Concatenation of tokens in variadic macros

后端 未结 1 1923
终归单人心
终归单人心 2021-01-06 11:14

In C, is it possible to concatenate each of the variable arguments in a a variadic macro?

Example:

MY_MACRO(A, B, C) // will yield HDR_A, HDR_B, HD         


        
相关标签:
1条回答
  • 2021-01-06 11:25

    First, the comma rule you are mentioning is a gcc extension, standard C doesn't have it and most probably will never have it since the feature can be achieved by different means.

    What you are looking for is meta programming with macros, which is possible, but you'd need some tricks to achieve that. P99 provides you with tools for that:

    #define MY_PREFIX(NAME, X, I) P99_PASTE2(NAME, X)
    #define MY_MACRO(...) P99_FOR(HDR_, P99_NARG(__VA_ARGS__), P00_SEQ, MY_PREFIX, __VA_ARGS__)
    
    • Here MY_PREFIX describes what has to be done with the individual items.
    • P00_SEQ declares how the items should be separated
    • P99_NARGS just counts the number of arguments
    0 讨论(0)
提交回复
热议问题