Creating Templates with Variable Number of Parameters

How can I create a template that works with an arbitrary number of type parameters?

Creating a template that works with an arbitrary number of type parameters is possible using variadic templates, a feature introduced in C++11. Variadic templates allow you to define templates that can take any number of arguments.

Here's an example of how to create and use a variadic template:

#include <iostream>
#include <tuple>

// Variadic template function
template <typename... Args>
void PrintAll(Args... args) {
  (std::cout << ... << args) << '\n';
}

// Variadic template class
template <typename... Types>
class Tuple {
 private:
  std::tuple<Types...> data;

 public:
  Tuple(Types... args) : data(args...) {}

  template <std::size_t N>
  auto get() {
    return std::get<N>(data);
  }

  void PrintTypes() {
    PrintTypesHelper<0, Types...>();
  }

 private:
  template <std::size_t N, typename T, typename... Rest>
  void PrintTypesHelper() {
    std::cout << typeid(T).name() << ' ';
    if constexpr (sizeof...(Rest) > 0) {
      PrintTypesHelper<N + 1, Rest...>();
    }
  }
};

int main() {
  // Using variadic template function
  PrintAll(1, 2.5, "Hello", 'a');

  // Using variadic template class
  Tuple<int, double, std::string> myTuple(
    10, 3.14, "World");
  std::cout << "First element: "
    << myTuple.get<0>() << '\n';
  std::cout << "Types in tuple: ";
  myTuple.PrintTypes();
}
12.5Helloa
First element: 10
Types in tuple: int double class std::string

Let's break down the key components:

  1. The ellipsis (...) is used to declare a parameter pack, which represents zero or more template parameters.
  2. In the PrintAll function, we use a fold expression ((std::cout << ... << args)) to print all arguments.
  3. The Tuple class uses variadic templates to create a tuple-like structure that can hold any number of elements of different types.
  4. The PrintTypes function uses recursion to print the types of all elements in the tuple.
  5. In main(), we demonstrate how to use both the variadic template function and class.

Variadic templates are powerful but can be complex. Here are some key points to remember:

  • You can use sizeof...(Args) to get the number of arguments in a parameter pack.
  • Parameter packs can be expanded in various contexts, like function arguments, template arguments, or initializer lists.
  • Recursive techniques are often used to process all elements in a parameter pack.

Variadic templates enable you to write highly generic and flexible code, but they can also make code harder to read and debug. Use them judiciously, and always consider whether a simpler solution might suffice for your needs.

Class Templates

Learn how templates can be used to create multiple classes from a single blueprint

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Partial Specialization of Class Templates
Is it possible to partially specialize a class template?
Compile-Time Polymorphism with Templates
Can I use templates to implement compile-time polymorphism?
Default Values for Non-Type Template Parameters
Is it possible to have default values for non-type template parameters?
Constraining Template Arguments
How can I create a template that only accepts certain types of arguments?
Typename vs Class in Template Declarations
What's the difference between typename and class in template parameter declarations?
Creating Class Templates with Multiple Types
How can I create a class template that works with both primitive types and user-defined types?
Best Practices for Naming Template Parameters
What are the best practices for naming template parameters?
Specializing Class Templates
How can I specialize a class template for specific types?
Using Template Parameters in Constructors
Can I use template parameters in the constructor of a class template?
Template Methods in Non-Template Classes
How do I create a template method within a non-template class?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant