vendredi 19 mars 2021

Generic functions vs Normal functions (templates in c++)

//Funtion template to return maximum of two numbers
template<typename T>

int myMax(int x,int y)
{
    cout<<"I am normal function"<<endl;
    return (x>y)?x:y;
}

T myMax(T x,T y)
{
    cout<<"I am templatised function"<<endl;
    return(x>y)?x:y;
}


int main()
{
    cout<<myMax<int>(5,7)<<endl;
    cout<<myMax(5,7)<<endl;

    return 0;
}

When I am compiling the above code, it is giving errors like 'T' does not name a type and no matching function for call to 'myMax(int, int)', Can anyone explain why these errors are coming and what syntactical errors I am doing here.The above code runs fine if I put generic function above the normal function. Please explain this behaviour also?

Aucun commentaire:

Enregistrer un commentaire