How do I declare a variable number of template arguments?

For example, this ad boost::tuple

// - tuple forward declaration -----------------------------------------------
template <
  class T0 = null_type, class T1 = null_type, class T2 = null_type,
  class T3 = null_type, class T4 = null_type, class T5 = null_type,
  class T6 = null_type, class T7 = null_type, class T8 = null_type,
  class T9 = null_type>
class tuple;

      

As expected, I get the following error if I try to use more arguments

$ g++ vec.cc 
vec.cc: In function 'int main()':
vec.cc:6: error: wrong number of template arguments (12, should be 10)
/usr/include/boost/tuple/detail/tuple_basic.hpp:75: error: provided for 'template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9> class boost::tuples::tuple'
vec.cc:6: error: template argument 1 is invalid
vec.cc:6: error: template argument 2 is invalid
vec.cc:6: error: invalid type in declaration before ';' token
$

      

Is there a way to declare a class to accept any number of template arguments?

+3


source to share


1 answer


C ++ 11 supports variadic templates . This allows you to write:

template<typename ...Args>
class tuple
{
    // ...
};

      



However, there is no easy way to iterate over the variational template arguments. See the linked article for several workarounds for this problem.

+3


source







All Articles