Function Templates with Multiple Parameters of different types (in same function)
We can use more than one generic type in the template function by using the comma to separate the list.
Syntax:
template<class T1, class T2,.....>
return_type function_name (arguments of type T1, T2....)
{
// body of function.
}
In the above syntax, we have seen that the template function can accept any number of arguments of a different type.
Example:
#include <iostream>
using namespace std;
template<class X,class Y> void fun(X a,Y b)
{
std::cout << "Value of a is : " <<a<< std::endl;
std::cout << "Value of b is : " <<b<< std::endl;
}
int main()
{
fun(15,12.3);
return 0;
}
/*
Output :
Value of a is : 15
Value of b is : 12.3
*/
In the above example, we use two generic types in the template function, i.e., X
and Y
.