当不支持可变参数模板参数时,如何为元组专门化一个类模板? [英] How to specialize a class template for a tuple when variadic template arguments are not supported?
本文介绍了当不支持可变参数模板参数时,如何为元组专门化一个类模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个班级模板
template<class T>
class A
{...};
当 T 是一个元组时,我想专门化它.我想我可以做到这一点
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?
推荐答案
您可以将其专门用于各种不同数量的元组:
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.
这篇关于当不支持可变参数模板参数时,如何为元组专门化一个类模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文