Variadic function templates in C++
Last Updated :
25 Nov, 2021
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor and Jaakko Järvi came up with this feature for C++.
Variadic arguments are very similar to arrays in C++. We can easily iterate through the arguments, find the size(length) of the template, can access the values by an index, and can slice the templates too.
So basically, Variadic function templates are functions that can take multiple numbers of arguments.Syntax:
template(typename arg, typename... args)
return_type function_name(arg var1, args... var2)
Note: The arguments must be put inside angular brackets.
Below is an example in C++ to show how we can use a variadic function template:
CPP
// C++ program to demonstrate working of
// Variadic function Template
#include <iostream>
using namespace std;
// To handle base case of below recursive
// Variadic function Template
void print()
{
cout << "I am empty function and "
"I am called at last.\n";
}
// Variadic function Template that takes
// variable number of arguments and prints
// all of them.
template <typename T, typename... Types>
void print(T var1, Types... var2)
{
cout << var1 << endl;
print(var2...);
}
// Driver code
int main()
{
print(1, 2, 3.14,
"Pass me any "
"number of arguments",
"I will print\n");
return 0;
}
Output1
2
3.14
Pass me any number of arguments
I will print
I am empty function and I am called at last.
Remember that templates are replaced by actual functions by the compiler.Explanation: The variadic templates work as follows : The statement, print(1, 2, 3.14, "Pass me any number of arguments", "I will print\n"); is evaluated in the following manner: Firstly, the compiler resolves the statement into
cout<< 1 <<endl ;
print(2, 3.14, "Pass me any number of arguments",
"I will print\n");
Now, the compiler finds a print() function which can take those arguments and in result executes the variadic print() function again in a similar manner:
cout<< 2 <<endl ;
print(3.14, "Pass me any number of arguments",
"I will print\n");
Again, it is resolved into the following forms :
cout<< 3.14 <<endl ;
print("Pass me any number of arguments",
"I will print\n");
cout<< "Pass me any number of arguments" <<endl ;
print("I will print\n");
cout<< "I will print\n" <<endl ;
print();
Now, at this point, the compiler searches for a function overload whose match is the empty function i.e. the function which has no argument. This means that all functions that have 1 or more arguments are matched to the variadic template and all functions that with no argument are matched to the empty function.
Similar Reads
C++ STL Algorithm Library Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste
3 min read
sort() in C++ STL In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
Type Inference in C++ (auto and decltype) Type Inference is a feature in C++, using which the compiler automatically deduces the data type of an expression, function, or variable. Type inference was introduced with C++11 through the use of the auto and decltype.Before C++ 11, each data type had to be explicitly declared, which limited the v
5 min read
transform() in C++ STL In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
Variadic function templates in C++ Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor
3 min read
Template Specialization in C++ Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p
5 min read
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge, Some of the merge operation classes are provided in C++ STL under the header file "algorithm", which facilitates several merge operations in a easy manner. Some of them are mentioned below. merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container
7 min read
std::partition in C++ STL C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa
5 min read
accumulate() and partial_sum() in C++ STL accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> hea
4 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota()) The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc.
4 min read