Is it possible in C to simultaneously recurse over multiple (in my case two) parameter packs? How?
To give a better idea of exactly what I'm asking, I currently have a class template as follows:
template<template<typename> class C, typename... Rest>
class Inherit {};
template<template<typename> class C, typename T, typename... Rest>
class Inherit<C, T, Rest...> :
public C<T>,
public Inherit<C, Rest...>
{
};
I'm using this class template in another class which takes a parameter pack Ts
to have that class inherit multiple times from a templated interface. For example:
template<typename T>
class Interface
{
...
};
template<typename... Ts>
class Example :
public Inherit<Interface, Ts...>
{
};
Now, what I would like to do is this:
template<typename T, const char* N>
class Interface
{
...
};
template<typename... Ts, const char*... Ns>
class Example :
public Inherit<Interface, Ts..., Ns...>
{
};
I need a templated Inherit
class that can recurse and pick an element off of both the Ts
and the Ns
parameter pack at once (assuming that parameter pack Ts
and Ns
have the same length). I'm not versed enough in template programming to know if this is even possible?
CodePudding user response:
I would argue that you don't need this Inherit
class template based on the examples you gave. Here's how I'd write your first example:
template<typename T>
class Interface
{
...
};
template<typename... Ts>
class Example : public Interface<Ts>...
{
};
and your second example:
template<typename T, const char* N>
class Interface
{
};
template<typename, const char*...>
class Example;
template<typename... Ts, const char*... Ns>
class Example<std::tuple<Ts...>, Ns...> : public Interface<Ts, Ns>...
{
};
CodePudding user response:
template <typename... Ts, const char*... Ns> class Example;
is wrong.
You might want something like (no recursion)
template<typename T, const char* N>
class Interface
{
// ...
};
template<typename T, typename Names> class Example;
template<typename... Ts, const char*... Ns>
class Example<std::tuple<Ts...>, std::integer_sequence<const char*, Ns...>> :
public: Interface<Ts, Ns>...
{
};
Interface<Ts, Ns>...
requires that both packs have the same size.