考虑一个可变参数宏__VA_ARGS__
中奇数位代表参数类型,偶数位代表参数名,想要提取__VA_ARGS__
中所有的偶数位的参数名,该怎么实现呢?
利用上一篇博客《c/c++:计算可变参数宏 __VA_ARGS__
的参数个数》的成果可以获取__VA_ARGS__
中参数的个数。在这个基础上添加一系列宏定义就可以实现,下面是完整代码及测试用例, gcc下测试通过:
// 计算 __VA_ARGS__ 参数个数,最大支持64个参数
#define FL_ARG_COUNT(...) FL_INTERNAL_ARG_COUNT_PRIVATE(0, ##__VA_ARGS__,\
64, 63, 62, 61, 60, \
59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define FL_INTERNAL_ARG_COUNT_PRIVATE(\
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, \
_10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \
_20, _21, _22, _23, _24, _25, _26, _27, _28, _29, \
_30, _31, _32, _33, _34, _35, _36, _37, _38, _39, \
_40, _41, _42, _43, _44, _45, _46, _47, _48, _49, \
_50, _51, _52, _53, _54, _55, _56, _57, _58, _59, \
_60, _61, _62, _63, _64, N, ...) N
// 参数拼接
#define FL_CONCAT_(l,r) l ## r
#define FL_CONCAT(l,r) FL_CONCAT_(l,r)
#define FL_ARG0()
#define FL_ARG2(t,v) v
#define FL_ARG4(t,v,...) FL_ARG2(t,v),FL_ARG2(__VA_ARGS__)
#define FL_ARG6(t,v,...) FL_ARG2(t,v),FL_ARG4(__VA_ARGS__)
#define FL_ARG8(t,v,...) FL_ARG2(t,v),FL_ARG6(__VA_ARGS__)
#define FL_ARG10(t,v,...) FL_ARG2(t,v),FL_ARG8(__VA_ARGS__)
#define FL_ARG12(t,v,...) FL_ARG2(t,v),FL_ARG10(__VA_ARGS__)
#define FL_ARG14(t,v,...) FL_ARG2(t,v),FL_ARG12(__VA_ARGS__)
#define FL_ARG16(t,v,...) FL_ARG2(t,v),FL_ARG14(__VA_ARGS__)
#define FL_ARG18(t,v,...) FL_ARG2(t,v),FL_ARG16(__VA_ARGS__)
#define FL_ARG20(t,v,...) FL_ARG2(t,v),FL_ARG18(__VA_ARGS__)
#define FL_ARG22(t,v,...) FL_ARG2(t,v),FL_ARG20(__VA_ARGS__)
#define FL_ARG24(t,v,...) FL_ARG2(t,v),FL_ARG22(__VA_ARGS__)
#define FL_ARG26(t,v,...) FL_ARG2(t,v),FL_ARG24(__VA_ARGS__)
#define FL_ARG28(t,v,...) FL_ARG2(t,v),FL_ARG26(__VA_ARGS__)
#define FL_ARG30(t,v,...) FL_ARG2(t,v),FL_ARG28(__VA_ARGS__)
#define FL_ARG32(t,v,...) FL_ARG2(t,v),FL_ARG30(__VA_ARGS__)
#define FL_ARG34(t,v,...) FL_ARG2(t,v),FL_ARG32(__VA_ARGS__)
#define FL_ARG36(t,v,...) FL_ARG2(t,v),FL_ARG34(__VA_ARGS__)
#define FL_ARG38(t,v,...) FL_ARG2(t,v),FL_ARG36(__VA_ARGS__)
#define FL_ARG40(t,v,...) FL_ARG2(t,v),FL_ARG38(__VA_ARGS__)
#define FL_ARG42(t,v,...) FL_ARG2(t,v),FL_ARG40(__VA_ARGS__)
#define FL_ARG44(t,v,...) FL_ARG2(t,v),FL_ARG42(__VA_ARGS__)
#define FL_ARG46(t,v,...) FL_ARG2(t,v),FL_ARG44(__VA_ARGS__)
#define FL_ARG48(t,v,...) FL_ARG2(t,v),FL_ARG46(__VA_ARGS__)
#define FL_ARG50(t,v,...) FL_ARG2(t,v),FL_ARG48(__VA_ARGS__)
#define FL_ARG52(t,v,...) FL_ARG2(t,v),FL_ARG50(__VA_ARGS__)
#define FL_ARG54(t,v,...) FL_ARG2(t,v),FL_ARG52(__VA_ARGS__)
#define FL_ARG56(t,v,...) FL_ARG2(t,v),FL_ARG54(__VA_ARGS__)
#define FL_ARG58(t,v,...) FL_ARG2(t,v),FL_ARG56(__VA_ARGS__)
#define FL_ARG60(t,v,...) FL_ARG2(t,v),FL_ARG58(__VA_ARGS__)
#define FL_ARG62(t,v,...) FL_ARG2(t,v),FL_ARG60(__VA_ARGS__)
#define FL_ARG64(t,v,...) FL_ARG2(t,v),FL_ARG62(__VA_ARGS__)
// 提取动态参数表中的偶数位参数,比如 一个参数序列:1,2,3,4,返回 2,4,最大支持64个参数
// 参数个数为奇数时会导致编译报错
#define FL_EVEN_ARG_(...) FL_CONCAT(FL_ARG,FL_ARG_COUNT(__VA_ARGS__))(__VA_ARGS__)
#define FL_EVEN_ARG(...) FL_EVEN_ARG_(__VA_ARGS__)
/* test case */
static const int a0[] = {FL_EVEN_ARG()}; // 返回空
static const int a1[] ={FL_EVEN_ARG(1,2)}; // 2
static const int a4[] ={FL_EVEN_ARG(1,2,3,4)}; // 2,4
static const int a20[] ={FL_EVEN_ARG(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)}; // 2,4,6,8,10,12,14,16,18,20