In modern C++, a template is a simple and very powerful statement that defines the operations of a class or function. Templates are parameterized features of C++. In this article, we will explain what a variadic template is and how you can use parameter pack in the templates of modern C++. You can download C++ and follow along too.
First of all, let’s try to explain what a template is in C++,
Table of Contents
What is a template in C++?
A template is a very powerful statement in C++ that simply defines the operations of a class, a function, an alias, or a variable. It lets the user apply the same template on different types to increase code reuse. Templates are like macros in C++, except the compiler checks the types used before the template is expanded. In the compilation mechanism of a template in C++, the source code contains only a template for a function or class, but when it is compiled, the same template can be used on multiple data types.
Here is the syntax of a template.
1 2 3 |
template < parameters > declaration |
The parameters of a template can be,
- a type template parameter,
- a non-type template parameter,
- a template template parameter (a template used as a parameter).
What is a variadic template in C++?
A type template parameter (typename or class) is a type passed as a parameter within the template parameters list. It is a typename or class. Thus, the user may use different type names or classes for the different declarations with templates.
A Variadic template is a template that has at least one parameter pack, that has a template parameter with the … (3 dots) ellipsis symbol that can be used to use templates with no arguments or with one or more arguments. These arguments can be non-types like (5, 9.81, “string”), or types like (int
, char
, float
, string
, class
) or templates.
A template parameter pack is a template parameter with … (3 dots) ellipsis symbol that can be used with more template arguments (non-types, types, or templates) or it can be zero. Parameter pack symbol can be used as a function parameter pack too, it can be zero or it may have more function arguments.
Sometimes, when we use a template we may use more template arguments. declare a template we use type template parameter with a default, in this way, you can easily use templates with default option, or you can define your class name.
Here is the syntax for the ‘type template parameter pack’,
1 2 3 |
template < type_parameter_key ... name(optional) > declaration |
A type parameter can be one of the following types,
- a typename
- a class
- a concept (since C++20)
For example we can declare a template with class parameter pack as below,
1 2 3 4 5 |
template<class ... T> class my_temp { }; |
The name is optional so if we don’t need class name in your template we can use it with parameter pack as below too,
1 2 3 4 5 |
template<class ...> class my_temp { }; |
How to use a variadic template in C++?
We can use many parameters if the template is a variadic template as below. We can use it without a parameter, with a single parameter, or more. Here is an example.
1 2 3 4 5 |
my_temp<> class_noparam; // the parameter list is empty my_temp<int> class_int; // the parameter list has one item my_temp<int, float, std::string> class_multiparam; // the parameter list has three items |
Is there an example of a variadic template in C++?
Here is an example to the ‘variadic template in C++’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> template<typename ... Targs> class my_temp { }; my_temp<void> class_noparam; // the parameter list is empty my_temp<int> class_int; // the parameter list has one item my_temp<int, float, std::string> class_multiparam; // the parameter list has three items int main() { } |
You can use variadic templates in C++ Builder.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition