Sunday, 10 July 2016

How template function or class is works internally?

 
Consider the following template function in your program

template<class T>
void printme(T a)
{
std::cout<<a<<std::endl;
}

Now assume that you have called this function 3 times in your program as follows,

printme<int>(5);
printme<int>(48);
printme<double>(4.54);

Here template function printme is used for types int and double in the program. So compiler will internally create an two functions printme as follows


void printme(int a)
{
std::cout<<a<<std::endl;
}

void printme(double a)
{
std::cout<<a<<std::endl;
}