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:
...
) is used to declare a parameter pack, which represents zero or more template parameters.PrintAll
function, we use a fold expression ((std::cout << ... << args)
) to print all arguments.Tuple
class uses variadic templates to create a tuple-like structure that can hold any number of elements of different types.PrintTypes
function uses recursion to print the types of all elements in the tuple.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:
sizeof...(Args)
to get the number of arguments 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.
Learn how templates can be used to create multiple classes from a single blueprint