[英]Split variadic arguments of a macro into pairs
Suppose I want to treat the arguments of a macro as pairs and then pass it to something else (expansively)假设我想将宏的 arguments 视为对,然后将其传递给其他东西(扩展地)
I imagine it looking something like this (pseudocode):我想它看起来像这样(伪代码):
template<unsigned index,
typename type>
struct my_pair
{
static constexpr auto i = index;
using t = type;
};
#define pair_off(...) my_pair<__VA_ARGS__[0], __VA_ARGS__[1]>, ...
With a variant of code from Is it possible to iterate over arguments in variadic macros?使用来自Is it possible to iterate over arguments in variadic macros 的代码变体? , you might do:
,你可以这样做:
#define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2) arg1##arg2
#define FOR_EACH_BY_PAIR_2(what, x, y)\
what(x, y)
#define FOR_EACH_BY_PAIR_4(what, x, y, ...)\
what(x, y)\
FOR_EACH_BY_PAIR_2(what, __VA_ARGS__)
#define FOR_EACH_BY_PAIR_6(what, x, y, ...)\
what(x, y)\
FOR_EACH_BY_PAIR_4(what, __VA_ARGS__)
#define FOR_EACH_BY_PAIR_8(what, x, y, ...)\
what(x, y)\
FOR_EACH_BY_PAIR_6(what, __VA_ARGS__)
#define NARG(...) NARG_(__VA_ARGS__, RSEQ_N())
#define NARG_(...) ARG_N(__VA_ARGS__)
#define ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
#define FOR_EACH_BY_PAIR_(N, what, ...) CONCATENATE(FOR_EACH_BY_PAIR_, N)(what, __VA_ARGS__)
#define FOR_EACH_BY_PAIR(what, ...) FOR_EACH_BY_PAIR_(NARG(__VA_ARGS__), what, __VA_ARGS__)
Limit is currently 8, up to you to increase that.目前限制为 8,您可以自行增加。
and then, you specific part:然后,你的具体部分:
#define MAKE_PAIR(x, y) COMMA my_pair<x, y>{}
#define COMMA ,
#define pair_off(p1, p2, ...) my_pair<p1, p2>{} FOR_EACH_BY_PAIR(MAKE_PAIR, __VA_ARGS__)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.