Ghostrider
Ghostrider

Reputation: 7775

reference variadic template parameter pack from another template

I have a variadic template class that defines collection of types

template <typename ... Types> class TypePack { };

Which is instantiated several times

typedef TypePack<T1, T2, T3> Pack1;
typedef TypePack<T1, T2, T4> Pack2;

I want to reference TypePack parameters from other templates

template <typename Pack> Client {
    static constexpr std::array<Foo, sizeof...(Pack::Types)> foos {
        make_foo<Pack::Types>()... 
    };
};
typedef Client<Pack1> Client1;
typedef Client<Pack2> Client2;

The code above is clearly wrong and doesn't compile. It just serves as illustration of what I would like to achieve.

I could define Pack1 and Pack2 through macros but I have a feeling that it should be possible to do it with variadic templates in C++14

Upvotes: 1

Views: 277

Answers (1)

Barry
Barry

Reputation: 303027

What you're looking for is partial specialization:

template <typename Pack> struct Client;

template <class... Ts>
struct Client<TypePack<Ts...>>
{
    static constexpr std::array<Foo, sizeof...(Ts)> foos {{ 
        make_foo<Ts>()...
    }};
};

// don't forget the definition
template <class... Ts>
constexpr std::array<Foo, sizeof...(Ts)> Client<TypePack<Ts...>>::foos;

Upvotes: 7

Related Questions