Variadic Function Templates in C++

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

GeeksforGeeks

A computer science portal for Custom Search


geeks
Practice GATE CS Placements Videos Contribute
Login/Register

Variadic function
templates in C++
Variadic templates are template that take a 3.9
variable number of arguments. Variadic
function templates are functions which can
take multiple number of arguments.

Syntax for a variadic function template: :

template(typename arg, typename... args)


return_type function_name(arg var1, args...
var2)

Note :, typename arg, typename... args must


be
inside angular brackets.

Below is an example in C++ to show how we


can use variadic function template:

// C++ program to demonstrate working of


// Variadic function Template
#include <iostream>
using namespace std;
 
// To handle base case of below recursiv
e
// 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 prin
ts
// 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;
}
Run on IDE

Output:

1
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 compiler.

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 following manner :
Firstly, the compiler resolves the statement into

cout<< 1 <<endl ;
print(2, 3.14, "Pass me any number of argum
ents",
"I will print\n");

Now, the compiler finds a print() function which


can take those arguments and in result
executes the variadic print() funciton again in
similar manner :
cout<< 2 <<endl ;
print(3.14, "Pass me any number of argument
s",
"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.

Reference :
http://www.cplusplus.com/articles/EhvU7k9E/

This article is contributed by MAZHAR IMAM


KHAN. If you like GeeksforGeeks and would
like to contribute, you can also write an article
using contribute.geeksforgeeks.org or mail
your article to contribute@geeksforgeeks.org.
See your article appearing on the
GeeksforGeeks main page and help other
Geeks.
Please write comments if you find anything
incorrect, or you want to share more
information about the topic discussed above.

GATE CS Corner    Company Wise


Coding Practice

C++ cpp-template Login to Improve this Article

Please write to us at contribute@geeksforgeeks.org


to report any issue with the above content.

Recommended Posts:

Template Specialization in C++


Templates in C++
A creative C++ program to Zoom digits of an
integer
Implementing Iterator pattern of a single
Linked List
Raw string literal in C++
vector::clear() and vector::erase() in C++ STL
deque::clear() and deque::erase() in C++ STL
std::istream_iterator and std::ostream_iterator
in C++ STL
Modulus of two float or double numbers
boost::split in C++ library

(Login to Rate)

Average Difficulty : 3.9/5.0


3.9 Based on 10 vote(s)
Add to TODO List
Mark as DONE
Basic Easy Medium Hard Expert

Writing code in comment? Please use


ide.geeksforgeeks.org, generate link and share
the link here.

Load Comments Share this post!


@geeksforgeeks, Some rights reserved      
 Contact Us!        About Us!           Careers!         
Privacy Policy    

You might also like