Home > Software design >  c template type pack specialization for tuple
c template type pack specialization for tuple

Time:04-12

When trying to implement a tuple type i run into the problem an empty tuple.
This is the type structure i used:

template <class T, class... Ts>
struct Tuple : public Tuple<Ts...> {};

template <class T>
struct Tuple {};

As soon as i try to add an overload for no type the compiler complains: Too few template arguments for class template 'Tuple':

template <> struct Tuple<> {};

I guess it's because the Tuple type was declared with at least one provided type at first and the compiler can't overload the same type with a different set of template parameters, but i wonder how i could solve this problem without completely restructuring my code.
My first idea was to define the tuple like template <class... Ts> struct Tuple {}; first and than add the other overloads, but the compiler than complains for to much template arguments.

CodePudding user response:

Your template expects at least one parameter. You can change it like this to allow zero or more:

template <typename ... Ts>
struct Tuple;

template <>
struct Tuple<> {};

template <class T,class... Ts>
struct Tuple<T,Ts...> : public Tuple<Ts...> {};

int main()
{
    Tuple<int,int,double> t;
}
  • Related