Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ template type pack specialization for tuple

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading