Real world use of C++ template type lists

624 Views Asked by At

I have built a variadic template type list

template <typename... Types>
struct type_list
{
};

and some modifiers functions like push_back

template <class TypeList, typename PushBack>
struct push_back
{
};

template <template<typename...> class TypeList, typename... Types, typename PushBack>
struct push_back<   TypeList<Types...>, PushBack    >
{
   using type = TypeList<Types..., PushBack>;
};

template <class TypeList, typename PushBack>
using push_back_t = typename push_back<TypeList, PushBack>::type;

Now I want to use type_list in the scope of a custom static reflection library to store all the meta struct holding the member variable infos(name, size, type, address). For example :

#define DECLARE_VARIABLE_INFO(VariableName) \
    using VariableName##_TypeInfo = VariableInfo<decltype(VariableName), VariableName##_Name, MemberAddress<&ClassType::VariableName>>; \
    *** What to put here to reuse the existing type_list without redefining a new list ***
    *** I could write using VariableName##_PushBack = push_back_t<MemberList, VariableName##_TypeInfo>; but MemberList will still be empty ***

struct Vec3
{
    float x, y, z = 0.f
    struct Vec3_Info // hold class meta info
    {
        using MemberList = meta_list::type_list<>;

        DECLARE_VARIABLE_INFO(x); // x_PushBack is there but MemberList is empty
        DECLARE_VARIABLE_INFO(y); // y_PushBack is there but it doesn't hold x meta info and MemberList is empty
        DECLARE_VARIABLE_INFO(z); // z_PushBack is there but it doesn't hold x and y meta infos and  MemberList is empty
    };
};

I can't understand how to fill existing type_list without redefining new types. Is it possible to do it ?
Am I using the wrong tool ? Or am I using, badly,the right tool ?
I'm sorry it is probably a dumb question, but I'm new to template meta programming and functional programming in general.

1

There are 1 best solutions below

1
On

You can do that:

// if VarInfo can be a single type
struct VarInfo
{
    //...

    template <class T>
    static constexpr VarInfo create(T var);
};

struct Vec3
{
    float x, y, z = 0.f

    static constexpr std::array<VarInfo, 3> var_infos = {    VarInfo::create(x),
                                                             VarInfo::create(y),
                                                             VarInfo::create(z)};
};