user1899020
user1899020

Reputation: 13575

How to specialize a class template for a tuple when variadic template arguments are not supported?

I have a class template

template<class T>
class A
{...};

and I want to specialize it when T is a tuple. I think I can do this

template<class Args...>
class A<std::tuple<Args...>>
{...};

However, my compiler doesn't support variadic template arguments, how to do it?

Upvotes: 4

Views: 1024

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

You can specialize it for tuples of every different arity:

// explicit specialization for 0-element tuples
template<>
class A<std::tuple<>>
{...};

// partial specialization for 1-element tuples
template<class Arg>
class A<std::tuple<Arg>>
{...};

// partial specialization for 2-element tuples
template<class Arg0, class Arg1>
class A<std::tuple<Arg0, Arg1>>
{...};

... and so on, up to whatever maximum number of tuple elements you need to support.

Upvotes: 1

Related Questions