Variadic function templates in C++

C++Server Side ProgrammingProgramming

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
raja
Published on 03-Apr-2019 12:40:50
Advertisements