是否可以创建一个宏来减少模板结构定义的样板,而模板结构只显示其所有模板参数?
template <
typename TPar1,
int TPar2,
...
bool TParN
>
struct MyStruct {
using Par1 = TPar1;
static int const Par2 = TPar2;
...
static bool const ParN = TParN;
};
请注意模板参数类型的不规则映射(typename
- &gt; using
,TYPE
- &gt; static TYPE const
)。我也会对某个无法自动映射这些类型的解决方案感到满意,但是例如只支持typename或者需要提示它是类型名还是类型。
答案 0 :(得分:1)
我找到了解决方案。 有些功劳归于this answer,用于计算传递的参数数量。
#define GET_1(p1, ...) p1
#define GET_2(p1, p2, ...) p2
#define GET_3(p1, p2, p3, ...) p3
#define GET_4(p1, p2, p3, p4, ...) p4
#define GET_5(p1, p2, p3, p4, p5, ...) p5
#define GET_6(p1, p2, p3, p4, p5, p6, ...) p6
#define GET_7(p1, p2, p3, p4, p5, p6, p7, ...) p7
#define GET_8(p1, p2, p3, p4, p5, p6, p7, p8, ...) p8
#define GET_9(p1, p2, p3, p4, p5, p6, p7, p8, p9, ...) p9
#define GET_10(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, ...) p10
#define GET_11(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, ...) p11
#define GET_12(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, ...) p12
#define MAP_1(f, del, pars) f( GET_1 pars)
#define MAP_2(f, del, pars) MAP_1(f, del, pars) del(dummy) f( GET_2 pars)
#define MAP_3(f, del, pars) MAP_2(f, del, pars) del(dummy) f( GET_3 pars)
#define MAP_4(f, del, pars) MAP_3(f, del, pars) del(dummy) f( GET_4 pars)
#define MAP_5(f, del, pars) MAP_4(f, del, pars) del(dummy) f( GET_5 pars)
#define MAP_6(f, del, pars) MAP_5(f, del, pars) del(dummy) f( GET_6 pars)
#define MAP_7(f, del, pars) MAP_6(f, del, pars) del(dummy) f( GET_7 pars)
#define MAP_8(f, del, pars) MAP_7(f, del, pars) del(dummy) f( GET_8 pars)
#define MAP_9(f, del, pars) MAP_8(f, del, pars) del(dummy) f( GET_9 pars)
#define MAP_10(f, del, pars) MAP_9(f, del, pars) del(dummy) f(GET_10 pars)
#define MAP_11(f, del, pars) MAP_10(f, del, pars) del(dummy) f(GET_11 pars)
#define MAP_12(f, del, pars) MAP_11(f, del, pars) del(dummy) f(GET_12 pars)
#define CONCAT_HELPER(x, y) x##y
#define CONCAT(x, y) CONCAT_HELPER(x, y)
#define MAP(f, del, pars) CONCAT(MAP_, NUM_TUPLE_ARGS(pars))(f, del, pars)
#define NUM_MACRO_ARGS(...) NUM_MACRO_ARGS_HELPER1(__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define NUM_MACRO_ARGS_HELPER1(...) NUM_MACRO_ARGS_HELPER2(__VA_ARGS__)
#define NUM_MACRO_ARGS_HELPER2(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) N
#define NUM_TUPLE_ARGS(tuple) NUM_MACRO_ARGS tuple
#define GET_ARG_PAR_NAME(par_type, decl_type, name) T##name
#define MAKE_TEMPLATE_PART_VA(...) GET_1(__VA_ARGS__) GET_ARG_PAR_NAME(__VA_ARGS__)
#define MAKE_TEMPLATE_PART(param_def) MAKE_TEMPLATE_PART_VA param_def
#define MAKE_STRUCT_PART_VA(...) GET_2(__VA_ARGS__) GET_3(__VA_ARGS__) = GET_ARG_PAR_NAME(__VA_ARGS__);
#define MAKE_STRUCT_PART(param_def) MAKE_STRUCT_PART_VA param_def
#define TYPE_ARG(name) (typename, using, name)
#define VALUE_ARG(type, name) (type, static type const, name)
#define DELIMITER_TEMPLATE_PART(dummy) ,
#define DELIMITER_STRUCT_PART(dummy)
#define DEFINE_ALIAS_STRUCT(name, pars) template < MAP(MAKE_TEMPLATE_PART, DELIMITER_TEMPLATE_PART, pars) > struct name { MAP(MAKE_STRUCT_PART, DELIMITER_STRUCT_PART, pars) };
DEFINE_ALIAS_STRUCT(MyStruct3, (
TYPE_ARG(Par1),
VALUE_ARG(bool, Par2),
TYPE_ARG(Par3),
TYPE_ARG(Par4),
TYPE_ARG(Par5),
TYPE_ARG(Par6),
TYPE_ARG(Par7),
TYPE_ARG(Par8),
TYPE_ARG(Par9)
))
此示例扩展为:
template <
typename TPar1,
bool TPar2,
typename TPar3,
typename TPar4,
typename TPar5,
typename TPar6,
typename TPar7,
typename TPar8,
typename TPar9
>
struct MyStruct3 {
using Par1 = TPar1;
static bool const Par2 = TPar2;
using Par3 = TPar3;
using Par4 = TPar4;
using Par5 = TPar5;
using Par6 = TPar6;
using Par7 = TPar7;
using Par8 = TPar8;
using Par9 = TPar9;
};