Variadic function templates in C++


Variadic function templates in C++ is a function which can take a multiple number of arguments.

Syntax

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

Example Code

 Live Demo

#include <iostream>
using namespace std;
void show() //base case. {
   cout << "I am now empty";
}

template <typename T, typename... T2>// variadic function
void show(T v1, T2... v2) {
   cout << v1 << endl;
   show(v2...) ;
}

int main() {
   show(7, 6, 0.04, "hi ","I am variadic function","I will show\n");
   return 0;
}

Output

7
6
0.04
hi I am variadic function
I will show

I am now empty

Updated on: 30-Jul-2019

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements