Search code examples
c++templatesc++11aliasvariadic

Template Alias Difficulty


I have the following:

template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename... Accumulated, typename... Added, template <typename, T...> class Z, T... Is, 
std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
    Generate<T, End, Count + 1, typename Merge<P, P<Accumulated...>,
        typename AppendEachToPack<T, P, Added, Is...>::type...>::type,
        typename Merge<P, typename AppendEachToPack<T, P, Added, Is...>::type...>::type,
        Z<T, Is...>, Js...> {};

and because typename AppendEachToPack<T, P, Added, Is...>::type... is being computed twice, I want rewrite the above as

template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename... Accumulated, typename... Added, template <typename, T...> class Z, T... Is,
std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
    GenerateAlias<T, End, Count + 1, P, P<Accumulated...>, Z<T, Is...>,
    typename AppendEachToPack<T, P, Added, Is...>::type..., Js...> {};

where

template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename Output, typename Sequence, typename... Expanded, std::size_t... Js>
using GenerateAlias = Generate<T, End, Count, typename Merge<P, Output, Expanded...>::type,
typename Merge<P, Expanded...>::type, Sequence, Js...>;

But the two packs declaration typename... Expanded, std::size_t... Js is not accepted. So how do I achieve what I want to achieve?

In case you need it, here are the definitions of AppendToEachPack and Merge:

// Appending an element to a pack.
template <typename T, T t, typename> struct Append;

template <typename T, T t, template <T...> class Z, T... Is>  
struct Append<T, t, Z<Is...>> {  
    using type = Z<Is..., t>;
};

// Appending many elements to a pack one at a time.
template <typename T, template <typename...> class P, typename Pack, T... Is>
struct AppendEachToPack {
    using type = P<typename Append<T, Is, Pack>::type...>;
};

// Merging multiple packs of types into a single pack of types.
template <template <typename...> class P, typename... Packs> struct Merge;

template <template <typename...> class P, typename Pack>
struct Merge<P, Pack> {
    using type = Pack;
};

template <template <typename...> class P, typename... Ts, typename... Us>
struct Merge<P, P<Ts...>, P<Us...>> {
    using type = P<Ts..., Us...>;
};

template <template <typename...> class P, typename Pack1, typename Pack2, typename... Packs>
struct Merge<P, Pack1, Pack2, Packs...> {
    using type = typename Merge<P, Pack1, typename Merge<P, Pack2, Packs...>::type>::type;
};

Solution

  • Thanks to T.C.'s hint, here is one solution (tested to work correctly):

    // Helper struct to forward the pack wrapping the problem pack.
    template <template <typename...> class P, typename PackOfPacks, typename... OtherPacks>
    struct MergeMany;
    
    template <template <typename...> class P, typename... Packs, typename... OtherPacks>
    struct MergeMany<P, P<Packs...>, OtherPacks...> : Merge<P, Packs..., OtherPacks...> {};
    
    // Now the desired GenerateAlias.
    template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
        typename Output, typename Sequence, typename Expanded, std::size_t... Js>
    using GenerateAlias = Generate<T, End, Count, typename MergeMany<P, Expanded, Output>::type,
        typename MergeMany<P, Expanded>::type, Sequence, Js...>;
    
    // And now using GenerateAlias, so typename AppendEachToPack<T, P, Added, Is...>::type... is computed only once.
    template <typename T, std::size_t End, std::size_t Count, template <typename...> class P, typename... Accumulated,
    typename... Added, template <typename, T...> class Z, T... Is, std::size_t... Js>
    struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
        GenerateAlias<T, End, Count + 1, P, P<Accumulated...>, Z<T, Is...>,
        P<typename AppendEachToPack<T, P, Added, Is...>::type...>, Js...> {};