其他分享
首页 > 其他分享> > c – 在调用宏中有两个或多个参数的FOR_EACH宏

c – 在调用宏中有两个或多个参数的FOR_EACH宏

作者:互联网

下面是一些C宏代码,它模拟for循环以消除复制和粘贴的代码.

#define SEMICOLON ;
#define LOL(x) print(x)
#define LOLZ(...) FOR_EACH(LOL, SEMICOLON, ##__VA_ARGS__)

LOLZ("hi", "my", "friend", "!");

// result
print("hi"); print("my"); print("friend"); print("!");

而且我也可以显示创建这个宏的代码(我发现这个here on Stack Overflow):

#define EXPAND(x) x
#define FOR_EACH_1(what, delimiter, x, ...) what(x)
#define FOR_EACH_2(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_1(what, delimiter, __VA_ARGS__))
#define FOR_EACH_3(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_2(what, delimiter, __VA_ARGS__))
#define FOR_EACH_4(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_3(what, delimiter, __VA_ARGS__))
#define FOR_EACH_5(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_4(what, delimiter, __VA_ARGS__))
#define FOR_EACH_6(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_5(what, delimiter, __VA_ARGS__))
#define FOR_EACH_7(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_6(what, delimiter, __VA_ARGS__))
#define FOR_EACH_8(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_7(what, delimiter, __VA_ARGS__))
#define FOR_EACH_9(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_8(what, delimiter, __VA_ARGS__))
#define FOR_EACH_10(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_9(what, delimiter, __VA_ARGS__))
#define FOR_EACH_11(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_10(what, delimiter, __VA_ARGS__))
#define FOR_EACH_12(what, delimiter, x, ...)\
  what(x) delimiter\
  EXPAND(FOR_EACH_11(what, delimiter, __VA_ARGS__))
#define FOR_EACH_13(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_12(what, delimiter, __VA_ARGS__))
#define FOR_EACH_14(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_13(what, delimiter, __VA_ARGS__))
#define FOR_EACH_15(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_14(what, delimiter, __VA_ARGS__))
#define FOR_EACH_16(what, delimiter, x, ...)\
  what(x) delimiter \
  EXPAND(FOR_EACH_15(what, delimiter, __VA_ARGS__))

#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
#define FOR_EACH_NARG_(...) EXPAND(FOR_EACH_ARG_N(__VA_ARGS__))
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N
#define FOR_EACH_RSEQ_N() 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define CONCATENATE(x,y) x##y
#define FOR_EACH_(N, what, delimiter, ...) EXPAND(CONCATENATE(FOR_EACH_, N)(what, delimiter, __VA_ARGS__))


#define FOR_EACH(what, delimiter, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, delimiter, __VA_ARGS__)

但是我对这个函数的两个参数有困难:
我需要在这个宏的一次迭代中使用func(x,y).
调用宏必须看起来像:

MY_DUAL_FOREACH_LIKE_MACRO(
  x1, y1,
  x2, y2,
  x3, y3
)

// and I expect to get:
func(x1, y1); func(x2, y2); func(x3, y3); 

如果我在宏的“x”参数附近添加“y”,我将获得调用“what”参数的x2 unsed替换:

func(x1, y1); func(x2, y2); func(x3, y3); func(, ); func(, ); func(, ); 

如果您有这方面的经验,请帮我将这个宏重新编码为双参数类型FOR_EACH宏.

解决方法:

反对我更好的判断,我将回答这个问题(因为它在少数情况下很有用).我修改了你发布的代码,所以它需要两个参数(demo):

#include <iostream>

#define EXPAND(x) x
#define FOR_EACH_2(what, delimiter, x, y) what((x), (y))
#define FOR_EACH_4(what, delimiter, x, y, ...)\
  what((x), (y)) delimiter \
  EXPAND(FOR_EACH_2(what, delimiter, __VA_ARGS__))
#define FOR_EACH_6(what, delimiter, x, y, ...)\
  what((x), (y)) delimiter \
  EXPAND(FOR_EACH_4(what, delimiter, __VA_ARGS__))

#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
#define FOR_EACH_NARG_(...) EXPAND(FOR_EACH_ARG_N(__VA_ARGS__))
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, N, ...) N
#define FOR_EACH_RSEQ_N() 6, 5, 4, 3, 2, 1, 0
#define CONCATENATE(x,y) x##y
#define FOR_EACH_(N, what, delimiter, ...) EXPAND(CONCATENATE(FOR_EACH_, N)(what, delimiter, __VA_ARGS__))

#define FOR_EACH(what, delimiter, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, delimiter, __VA_ARGS__)

void foo(int x, float y) {
    std::cout << "foo(" << x << ", " << y << ")\n";
}

int main() {
    FOR_EACH(foo, ;, 1, 3.14, 2, 1.41, 3, 1.73);
}

输出是:

foo(1, 3.14)
foo(2, 1.41)
foo(3, 1.73)

如果你想调用foo超过3次,你将不得不添加其他案例,我将把这作为练习留给读者.

这可以被清理,并且可能应该给出与FOR_EACH不同的名称(可能类似于FOR_EACH_2以表示它一次对2个参数进行操作),我将其作为另一个练习留给读者.

我把这些东西作为练习留给读者的原因是不鼓励使用这个答案(但不能完全阻止它).如果你不能打扰清理它并根据你的需要量身定做,那就好了.

标签:substitution,c,foreach,macros,c-preprocessor
来源: https://codeday.me/bug/20190824/1705467.html