c++11variadic-templatestemplate-meta-programmingtypelist

Merge two variadic templates in one


Im trying to implement a variadic template wrapper of the loki typelist.

Merging two typelists in loki-style is easy, but im having problems with merge in variadic-template style.

This is my implementation (Simplified, without push_back , index_of , ... , methods).

template<typename... Ts>
struct dl32TypeList;

template<typename HEAD , typename... TAIL>
struct dl32TypeList<HEAD,TAIL...>
{
    static const unsigned int size = sizeof...(TAIL) + 1;

    using value = dl32Loki_like_TypeList<HEAD, typename dl32TypeList<TAIL...>::value>;
};

template<>
struct dl32TypeList<>
{
    using value = dl32NoType;

    static const unsignedint size = 0;
};

I want something like:

template<typename OTHER_TYPELIST> 
using merge = dl32TypeList<HEAD , TAIL... , typename OTHER_TYPELIST::???>;

And this is the problem: We cannot store variadic template args as a using/typedef, so i have any idea about how i can do this. (Note the OTHER_TYPELIST::???).


Solution

  • I don't know what Loki or DL32 are, and it's not clear you should be implementing anything at all.

    std::tuple is a common tool for type lists. It is designed as a runtime storage container but works as a compile-time utility as long as the types are complete. Here is one way to catenate tuples:

    template< typename ... t >
    struct tuple_cat
        { typedef decltype( std::tuple_cat( std::declval< t >() ... ) ) type; };
    

    If you want to do it manually, try partial specialization:

    template< typename ... t >
    struct type_list {};
    
    template< typename ta, typename tb >
    struct type_cat;
    
    template< typename ... a, typename ... b >
    struct type_cat< type_list< a ... >, type_list< b ... > >
        { typedef type_list< a ..., b ... > type; };
    

    As for the size member, you can make a universal metafunction to solve the problem once and for all.

    template< typename >
    struct count_types;
    
    template< template< typename ... > class t, typename ... a >
    struct count_types< t< a ... > >
        { static constexpr std::size_t value = sizeof ... a; };