0

例如,我有一堂课

class A : public B
{
public:
    template<unsigned i> struct Element
    {
         // typedef void Type; // will specialize it.
    };

    A(Element<0>::Type v0 = initial0,
      Element<1>::Type v1 = initial1,
      Element<2>::Type v2 = initial2,
      Element<3>::Type v3 = initial3
     )
    : B(v0,v1,v2,v3)
    {}

};

我想用宏来替换构造函数,比如

CONSTRUCT(A, initial0, initial1, initial2, initial3)

并且元素的数量是可变的(需要...可变参数宏)。

有可能做到吗?谢谢

4

1 回答 1

0

我想到了。

// Construct with defaults.
FOO(x)                                  x

#define _1_DEFAULTS(N, def)             Element<N-1>::Type const& value0 = def
#define _2_DEFAULTS(N, def, ...)        Element<N-2>::Type const& value1 = def, FOO(_1_DEFAULTS(N, __VA_ARGS__))
#define _3_DEFAULTS(N, def, ...)        Element<N-3>::Type const& value2 = def, FOO(_2_DEFAULTS(N, __VA_ARGS__))
#define _4_DEFAULTS(N, def, ...)        Element<N-4>::Type const& value3 = def, FOO(_3_DEFAULTS(N, __VA_ARGS__))
#define _5_DEFAULTS(N, def, ...)        Element<N-5>::Type const& value4 = def, FOO(_4_DEFAULTS(N, __VA_ARGS__))
#define _6_DEFAULTS(N, def, ...)        Element<N-6>::Type const& value5 = def, FOO(_5_DEFAULTS(N, __VA_ARGS__))
#define _7_DEFAULTS(N, def, ...)        Element<N-7>::Type const& value6 = def, FOO(_6_DEFAULTS(N, __VA_ARGS__))
#define _8_DEFAULTS(N, def, ...)        Element<N-8>::Type const& value7 = def, FOO(_7_DEFAULTS(N, __VA_ARGS__))
#define _9_DEFAULTS(N, def, ...)        Element<N-9>::Type const& value8 = def, FOO(_8_DEFAULTS(N, __VA_ARGS__))
#define _10_DEFAULTS(N, def, ...)       Element<N-10>::Type const& value9 = def, FOO(_9_DEFAULTS(N, __VA_ARGS__))

#define _1_VALUES                       value0
#define _2_VALUES                       value1, _1_VALUES
#define _3_VALUES                       value2, _2_VALUES
#define _4_VALUES                       value3, _3_VALUES
#define _5_VALUES                       value4, _4_VALUES
#define _6_VALUES                       value5, _5_VALUES
#define _7_VALUES                       value6, _6_VALUES
#define _8_VALUES                       value7, _7_VALUES
#define _9_VALUES                       value8, _8_VALUES
#define _10_VALUES                      value9, _9_VALUES

#define _NUM_ARGS2(X,X64,X63,X62,X61,X60,X59,X58,X57,X56,X55,X54,X53,X52,X51,X50,X49,X48,X47,X46,X45,X44,X43,X42,X41,X40,X39,X38,X37,X36,X35,X34,X33,X32,X31,X30,X29,X28,X27,X26,X25,X24,X23,X22,X21,X20,X19,X18,X17,X16,X15,X14,X13,X12,X11,X10,X9,X8,X7,X6,X5,X4,X3,X2,X1,N,...) N
#define NUM_ARGS(...) FOO(_NUM_ARGS2(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 _CONSTRUCT_V1(A, B, N, ...)     A(FOO(_##N##_DEFAULTS(N, __VA_ARGS__))) : B(_##N##_VALUES) {}
#define _CONSTRUCT_V2(A, B, N, ...)     FOO(_CONSTRUCT_V1(A, B, N, __VA_ARGS__))
#define CONSTRUCT(A, B, ...)            FOO(_CONSTRUCT_V2(A, B, NUM_ARGS(__VA_ARGS__), __VA_ARGS__))
于 2013-07-27T19:16:00.317 回答