Variadic Class Template Arguments -- Sandor Dargo
Variadic templates are a powerful C++ feature that allow template classes or functions to accept an arbitrary number of parameters. In this article, we’ll explore how to combine them with class templates and examine the various ways the parameter pack can be expanded.
Variadic Class Template Arguments
by Sandor Dargo
From the article:
Let’s talk about class templates and variadic parameters. How to use them in combination?
But first of all, what are variadic templates?
Variadic template don’t accept a fixed size of parameters, but anything from one to more. The set of parameters is called the parameter pack.
In the template parameter list, we can see the three dots (
...
) signaling the pack after thetypename
orclass
keywords, or after the constraints. Then later in the function / constructor parameter list, you can observe it right after the actual type name, and finally once the pack is expanded, it comes after the packed variable name.Variadic arguments can be used in so many different ways both with function and class templates. Today, we are focusing on class templates.
When you are using variadic templates with class templates, the expansion can happen at different places. You might expand the parameter pack
- within the constructor or any other member function
- when declaring a member variable
- at the inheritance list
- in a using declaration
- in friend declarations