我想在可变参数宏中为每个元素附加一个字符串化的宏参数。我想我知道我需要什么,但我还没有想出一个有效的解决方案。鉴于可变宏,如:
#define FIELD_DECLARATION(NAME, OTHER_FIELD, ...)
FIELD_DECLARATION(First, Thing)
FIELD_DECLARATION(Second, Thing, Thing, Nothing)
我想生成:
field_First = {ThingArg};
field_Second = {ThingArg, ThingArg, NothingArg};
我想我需要的是递归地继续扩展__VA_ARGS__
直到它没有元素,并在进行扩展时附加"Arg"
。最后,将结果传递给另一个可变参数宏,该宏生成以逗号分隔的参数列表。
我已经尝试了这个,这不会起作用(也不是我所描述的):
#define UNPACK_VA_1(A1) A1 ## Arg
#define UNPACK_VA_2(A1, A2) UNPACK_VA_1(A1), UNPACK_VA_1(A2)
#define UNPACK_VA_3(A1, A2, A3) UNPACK_VA_2(A1, A2), UNPACK_VA_1(A3)
#define UNPACK_VA_4(A1, A2, A3, A4) UNPACK_VA_2(A1, A2), UNPACK_VA_2(A3, A4)
#define UNPACK_VA(...) UNPACK_VA_4(__VA_ARGS__)
#define FOO(x, y, ...) UNPACK_VA(__VA_ARGS__)
FOO(One, Two, Three, Four, Five, Six)
虽然这有点奏效,但我无法提出可扩展的解决方案。如果有人可以发光,那就太好了。
答案 0 :(得分:7)
这是一种可扩展的方法。首先,一些通用实用程序宏:
#define EVAL(...) __VA_ARGS__
#define VARCOUNT(...) \
EVAL(VARCOUNT_I(__VA_ARGS__,9,8,7,6,5,4,3,2,1,))
#define VARCOUNT_I(_,_9,_8,_7,_6,_5,_4,_3,_2,X_,...) X_
#define GLUE(X,Y) GLUE_I(X,Y)
#define GLUE_I(X,Y) X##Y
#define FIRST(...) EVAL(FIRST_I(__VA_ARGS__,))
#define FIRST_I(X,...) X
#define TUPLE_TAIL(...) EVAL(TUPLE_TAIL_I(__VA_ARGS__))
#define TUPLE_TAIL_I(X,...) (__VA_ARGS__)
#define TRANSFORM(NAME_, ARGS_) (GLUE(TRANSFORM_,VARCOUNT ARGS_)(NAME_, ARGS_))
#define TRANSFORM_1(NAME_, ARGS_) NAME_ ARGS_
#define TRANSFORM_2(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_1(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_3(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_2(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_4(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_3(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_5(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_4(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_6(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_5(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_7(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_6(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_8(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_7(NAME_,TUPLE_TAIL ARGS_)
#define TRANSFORM_9(NAME_, ARGS_) NAME_(FIRST ARGS_),TRANSFORM_8(NAME_,TUPLE_TAIL ARGS_)
在语义上,VARCOUNT
计算参数; GLUE
是典型的间接贴纸; FIRST
提取第一个参数; EVAL
扩展为其参数(意图评估),TUPLE_TAIL
返回元组的尾部(即,它丢弃第一个参数)。
TRANSFORM
这是主要的想法; TRANSFORM(FOO,(X,Y,Z))
将元组(X,Y,Z)
带到(FOO(X),FOO(Y),FOO(Z))
。
这就是这里的特殊用途代码:
#define Z_ARG(X) GLUE(X,Arg)
#define MAKE_INITIALIZER(...) { __VA_ARGS__ }
#define FIELD_DECLARATION(FNAME_, ...) \
GLUE(field_, FNAME_) = EVAL(MAKE_INITIALIZER TRANSFORM(Z_ARG, (__VA_ARGS__)));
鉴于上述情况,这应该是可读的,但只是解释...... Z_ARG
粘贴Arg
到项目; MAKE_INITIALIZER
将预处理器元组转换为初始化列表;而FIELD_DECLARATION
是你的宏。请注意EVAL
包装MAKE_INITIALIZER
/转换后的元组,以便实际调用该宏。
注意:将EVAL
移至顶部并在更多位置使用它,这样也可以在MSVC中使用。
答案 1 :(得分:0)
我已经在Visual Studio C ++上测试了上面的代码,存在一些问题,并且编译器显示错误。 不需要一些专门用于 VA_ARGS 的小游戏,而且在某些地方,我们必须使用 VA_ARGS 和。 。代替了正确的代码“ Args ”,并且扩展到60个输入项是:
#ifndef __MACROS__
#define __MACROS__
#define EVAL(...) __VA_ARGS__
#define VARCOUNT(...) \
EVAL(VARCOUNT_I(__VA_ARGS__,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,))
#define VARCOUNT_I(_,_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,X_,...) X_
#define GLUE(X,Y) GLUE_I(X,Y)
#define GLUE_I(X,Y) X##Y
#define FIRST(...) EVAL(FIRST_I(__VA_ARGS__,))
#define FIRST_I(X,Args) X
#define TUPLE_TAIL(...) EVAL(TUPLE_TAIL_I(__VA_ARGS__))
#define TUPLE_TAIL_I(X,...) __VA_ARGS__
#define TRANSFORM(NAME_, ...) GLUE(TRANSFORM_,VARCOUNT(__VA_ARGS__))(NAME_, __VA_ARGS__)
#define TRANSFORM_1(NAME_, ARGS_) NAME_ (ARGS_)
#define TRANSFORM_2(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_1(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_3(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_2(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_4(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_3(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_5(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_4(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_6(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_5(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_7(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_6(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_8(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_7(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_9(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_8(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_10(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_9(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_11(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_10(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_12(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_11(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_13(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_12(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_14(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_13(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_15(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_14(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_16(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_15(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_17(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_16(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_18(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_17(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_19(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_18(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_20(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_19(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_21(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_20(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_22(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_21(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_23(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_22(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_24(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_23(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_25(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_24(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_26(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_25(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_27(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_26(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_28(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_27(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_29(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_28(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_30(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_29(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_31(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_30(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_32(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_31(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_33(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_32(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_34(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_33(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_35(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_34(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_36(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_35(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_37(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_36(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_38(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_37(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_39(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_38(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_40(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_39(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_41(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_40(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_42(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_41(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_43(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_42(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_44(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_43(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_45(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_44(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_46(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_45(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_47(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_46(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_48(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_47(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_49(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_48(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_50(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_49(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_51(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_50(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_52(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_51(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_53(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_52(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_54(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_53(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_55(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_54(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_56(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_55(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_57(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_56(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_58(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_57(NAME_,TUPLE_TAIL( ARGS_))
#define TRANSFORM_59(NAME_, ARGS_) NAME_(FIRST (ARGS_)) TRANSFORM_58(NAME_,TUPLE_TAIL( ARGS_))
#endif