Variadic class templates
Class templates may also have a variable number of template arguments. This is key to building some categories of types, such as tuple
and variant
, that are available in the standard library. In this section, we will see how we could write a simple implementation for a tuple
class. A tuple is a type that represents a fixed-size collection of heterogeneous values.
When implementing variadic function templates we used a recursion pattern with two overloads, one for the general case and one for ending the recursion. The same approach has to be taken with variadic class templates, except that we need to use specialization for this purpose. Next, you can see a minimal implementation for a tuple:
template <typename T, typename... Ts> struct tuple { tuple(T const& t, Ts const &... ts) : value(t), rest(ts...) { } constexpr int size() const...