C++ template with more than one parameters

Introduction

Templates can have more than one parameter.

We simply list the template parameters and separate them using a comma.

Example of a function template that accepts two template parameters:

#include <iostream> 

template <typename T, typename U> 
void myfunction(T t, U u) 
{ 
    std::cout << "The first parameter is: " << t << '\n'; 
    std::cout << "The second parameter is: " << u << '\n'; 
} 

int main() /*w ww.j a  va 2  s  .co  m*/
{ 
    int x = 123; 
    double d = 456.789; 
    myfunction<int, double>(x, d); 
} 



PreviousNext

Related