0

Is is possible to create a macro that will reduce the boilerplate for definitions of template structs which merely expose all their template parameters?

template <
    typename TPar1,
    int TPar2,
    ...
    bool TParN
>
struct MyStruct {
    using Par1 = TPar1;
    static int const Par2 = TPar2;
    ...
    static bool const ParN = TParN;
};

Note the irregular mapping of template parameter types (typename->using, TYPE->static TYPE const). I will also be somehow satisfied with a solution that cannot map these types automatically, but for example only supports typename or requires a hint whether it's a typename or a type.

Ambroz Bizjak
  • 7,809
  • 1
  • 38
  • 49
  • Give the "irregular mapping", it's hard to envisage a macro to do that. It wouldn't be too difficult create a little utility program which outputs the template definition based on an input list, and conditionally run it in your build script. I can accept there is an occasional need to create such "boilerplate" templates, but how often, realistically, do you anticipate that? – Peter Dec 28 '15 at 09:29
  • @Peter I seriously do this all over my code (see https://github.com/ambrop72/aprinter/blob/master/aprinter/printer/PrinterMain.h#L77). Yes, it's quite an unusual style of coding. Thanks for your consideration but I am really looking for a specific solution to this problem. – Ambroz Bizjak Dec 28 '15 at 09:34
  • Looking at that sample, it seems like you've gone the whole hog with making almost everything you can a template parameter. Looking at them, I really doubt they need to be made as generic as they are. As an example, how many distinct types have you used for `TLedBlinkInterval` and `TInactiveTime` when instantiating the `PrinterMainParams` template? Both (presumably) have units of time. – Peter Dec 28 '15 at 10:56
  • @Peter, These template parameters actually contain information related to the runtime configuration system, which is processed at compile time. I know it doesn't have to be that "complicated", whole thing is half an experiment in how far you can push template metaprogramming. Note that any further questioning of what I am doing is not really productive. – Ambroz Bizjak Dec 28 '15 at 11:22
  • I understood what you're using the parameters for, Ambroz. The question I asked is one that I have seen asked in professional code reviews when code like yours is submitted. I don't look at such questions as counter-productive - in fact, inability to explain such things is usually a sign of a deeper problem in a production setting. – Peter Dec 28 '15 at 11:35
  • @Peter I am sure able to explain the design. Basically I use lots of template metaprogramming to achieve various forms of "staticness" - statically allocated memory, the compiler knowing the constant addresses of objects, knowing the value of parameters which are always constant. Whether this is a smart thing to do is not subject of this question. – Ambroz Bizjak Dec 28 '15 at 11:40

1 Answers1

1

I have found a solution. Some credit goes to this answer for figuring out the number of parameters passed.

#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)
))

This example expands to:

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;
};
Community
  • 1
  • 1
Ambroz Bizjak
  • 7,809
  • 1
  • 38
  • 49