Lukas Engelke Lukas Engelke - 1 year ago 104
C++ Question

Size of parameter pack in template specialization

I have a template that provides a type for any given Order in the range from


template <class Graph, int Order> TypeHelper;

This is necessary because
TypeHelper<Graph, k>
depends on
TypeHelper<Graph, 0>
TypeHelper<Graph, k - 1>
TypeHelper<Graph, k + 1>

is a variadic template that serves as a container for the type of the
for any given order.

template <class... Payloads> Graph;

In order to terminate the recursion at
TypeHelper<Graph, 0>
TypeHelper<Graph, MaxOrder>
these are specialized. The former is straight forward but I cannot figure out how to derive
from the number of types in

One obvious solution is to introduce
as a template parameter of

As an example:

template <int MaxOrder, class... Payloads> Graph;

template <template <int, class...> class Graph, int MaxOrder, class... Payloads>
struct TypeHelper<Graph<MaxOrder, Payloads...>, MaxOrder>

But I would prefer something like this

template <template <class...> class Graph, class... Payloads>
struct TypeHelper<Graph<Payloads...>, sizeof...(Payloads)>

But this does not work.

Further reading: Is sizeof... allowed in template arguments for specialization?

Any suggestions?

Answer Source

I'm not sure I got the problem, but you can still use an intermediate class that inherits from your actual implementation:

template <class Graph, int Order>
struct TypeHelperImpl;

// specializations of TypeHelperImpl

template <typename...>
struct TypeHelper;

template <template <class...> class Graph, class... Payloads>
struct TypeHelper<Graph<Payloads...>, Payloads...>
    : TypeHelperImpl<Graph<Payloads...>, sizeof...(Payloads)> {};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download