【语法】宏定义重载

参考:

https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments?rq=1


#define GET_MACRO(_1,_2,_3,NAME,...) NAME
#define FOO(...) GET_MACRO(__VA_ARGS__, FOO3, FOO2)(__VA_ARGS__)

So if you have these macros:

FOO(World, !)         # expands to FOO2(World, !)
FOO(foo,bar,baz)      # expands to FOO3(foo,bar,baz)

If you want a fourth one:

#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define FOO(...) GET_MACRO(__VA_ARGS__, FOO4, FOO3, FOO2)(__VA_ARGS__)

FOO(a,b,c,d)          # expeands to FOO4(a,b,c,d)

其中VA_ARGS 就是: ···


F00(world,!)
解析为:
GET_MACRO(world,!,F003,F002)(world,!) 故:NAME 为F002

GET_MACRO(world,!,F003,F002) 被解析为F002

所以最终为F002(world,!)


F00(foo,bar,baz)
解析为:
GET_MACRO(foo,bar,baz,F003,F002)(foo,bar,baz) 故:NAME 为F003

GET_MACRO(foo,bar,baz,F003,F002) 被解析为F003

所以最终为F003(foo,bar,baz)


你可能感兴趣的:(【语法】宏定义重载)