vendredi 26 octobre 2018

Type conversion in template

I am using a templated function and a non-templated one. Code is given below

#include <iostream>
using namespace std;

int maxOfTwo1(int a,int b)
{
    return a>b?a:b;
}
template<class T>
T maxOfTwo(T a,T b)
{
    return a>b?a:b;
}
int main()
{
    cout<<maxOfTwo1(3,6.3);//works fine
    cout<<maxOfTwo(3,6.3);//gives error
}

In first function 6.3 is getting converted to 6, but can anyone explain why it doesn't happen in second function also? Second function works also the same just having template.

Aucun commentaire:

Enregistrer un commentaire