宏元编程——数据结构——tuple元组

时间:2021-01-07 18:39:30

tuple元组

tuple定义:(a,b,c,d...)
操作tuple宏函数: 1,TL_TUPLE_ELEM(size, i, tuple)宏可以获取元组里的第i个元素。

实现思路:可以通过宏元编程——可变参数的宏函数里的TL_VARIADIC_ELEM宏来求解。 要解决1个问题:如何tuple的括号去掉?
#define TL_TUPLE_ELEM(i, tuple) TL_TUPLE_ELEM_I(i, tuple)

#define TL_TUPLE_ELEM_I(i, tuple) TL_CAT(TL_VARIADIC_ELEM,(i, TL_TUPLE_ELEM_EAT_BRACKET(tuple)))

#define TL_TUPLE_ELEM_EAT_BRACKET(tuple) TL_TUPLE_ELEM_II tuple
#define TL_TUPLE_ELEM_II(...) __VA_ARGS__


TL_TUPLE_ELEM(0,(44,56,77,99)) => 44
2,TL_TUPLE_SIZE
#define TL_TUPLE_SIZE(tuple) TL_VARIADIC_SIZE##tuple