问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/19140246/how-to-specialize-a-class-template-for-a-tuple-when-variadic-template-arguments