Variadic Template in Cpp
What is The Variadic Template?
A variadic template in C++ is a type of template that allows a function or a class to take an arbitrary number of arguments of any type.
Variadic template means that the compiler uses a variable number of types and arguments.
The “…” token in C++ is used to represent a variadic argument list in a variadic template
In C++, variadic templates are implemented using the “…” syntax, which represents a variadic argument list.
C and C++ both support variadic functions, but they have different syntax and techniques for implementing them.
In C, variadic functions are implemented using the va_list, va_start, va_arg, and va_end macros. The va_list type is used to store a list of arguments, and the va_start macro initializes the list. The va_arg macro is used to retrieve the next argument from the list, and the va_end macro is used to clean up the list.
Here is an example of a simple variadic function in C:
In C++, variadic templates provide a more modern and flexible way of implementing variadic functions. With variadic templates, you can pass an arbitrary number of arguments of any type to a function or a class, and you can also use compile-time type checking to ensure that the correct types of arguments are being passed.
Variadic Template usage in C++
As known, Template means that codes is written by the compiler.
The variadic func() function is actually equivalent to the following code.
The code that the compiler will write for following usage is equal to func(int, double)
So can these parameters be referances? Of course as follows;
Sizeof() Operator for variadic templates
As known. sizeof() operators create constant expression in compile time.
The sizeof operator in C++ can be used to determine the size of a data type or an object. When used with variadic templates, sizeof can be used to determine the size of the elements in a variadic argument list.
Variadic Template sizeof() operator usage is as above.
Parameter Pack in Variadic Template
The Args expression can be consider of as the parameter names we give to the function, while the Ts expression holds the parameter types.
An example for usage variadic template in Cpp
let’s we are write a function that adding all the parameters we send.
But first a critical note;
if compiler encounter a variadic function overloading, it selects non-variadic function.
So how does it work?
As shown, sum function is overloaded. One of the overloaded functions has a one parameter that a template type parameter and the other has a template type parameter and template type package.
Return value of function is first + sum(args..) that is , the sum of first value and the return value of sum() function.
This technique is called the “recursive technique”.
Let’s we examine step by step;
to sum(10, 20, 30, 40)
first : 10
sum(args..) : 20 30 40
to sum(20, 30, 40)
first : 20
sum(args..) :30 40
to sum(30, 40)
first : 30
sum(T) :40 — — — — — -> compiler selects other overloaded function here.
So All steps is like below.