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

[亡魂溺海] 提交于 2019-12-14 01:28:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!