Overloading Macro on Number of Arguments

后端 未结 8 1558
离开以前
离开以前 2020-11-22 04:25

I have two macros FOO2 and FOO3:

#define FOO2(x,y) ...
#define FOO3(x,y,z) ...

I want to define a new macro

8条回答
  •  情歌与酒
    2020-11-22 04:48

    To add on to netcoder's answer, you CAN in fact do this with a 0-argument macro, with the help of the GCC ##__VA_ARGS__ extension:

    #define GET_MACRO(_0, _1, _2, NAME, ...) NAME
    #define FOO(...) GET_MACRO(_0, ##__VA_ARGS__, FOO2, FOO1, FOO0)(__VA_ARGS__)
    

提交回复
热议问题