Class Templates

Creating Templates with Variable Number of Parameters

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

Illustration representing computer hardware

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved