简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM