Overload C/C++ preprocessor macro on structure of its argument

后端 未结 2 1369
轮回少年
轮回少年 2021-02-09 18:09

I would like to write a preprocessor macro that does one thing if it\'s argument is a parenthesized tuple of tokens, like this:

MY_MACRO((x, y))
<
2条回答
  •  借酒劲吻你
    2021-02-09 18:24

    As for your first question, the following macros might meet your purpose:

    #define CONCAT_( x, y ) x ## y
    #define CONCAT( x, y ) CONCAT_( x, y )
    #define IS_SINGLE_1(...) 0
    #define IGNORE(...)
    #define IS_SINGLE_2_0           0 IGNORE(
    #define IS_SINGLE_2_IS_SINGLE_1 1 IGNORE(
    #define IS_SINGLE( x ) CONCAT( IS_SINGLE_2_, IS_SINGLE_1 x ) )
    IS_SINGLE((x, y)) // 0
    IS_SINGLE(x)      // 1
    

    Macro IS_SINGLE is expanded to 1 if the argument is single token, otherwise, 0.

    Hope this helps

提交回复
热议问题