How the compiler resolves the case in which it can substitute more than one template and does not give ambiguity using the SFINAE principle.
#include <stdio.h>
#include <iostream>
template<typename T, typename U>
U cast(T x){
return static_cast<U>(x);
}
template<typename T, typename U>
T cast(U x){
return static_cast<T>(x);
}
if we try this:
int main(int argc, char const *argv[])
{
std::cout << cast<int,float>(10) << "\n";
return 0;
}
is printed 10, however if we try:
int main(int argc, char const *argv[])
{
std::cout << cast<int,float>(10.34) << "\n";
return 0;
}
gives the following compilation error:
more than one instance of overloaded function "cast" matches the argument > list: - function template "U cast (T x)" - function template "T>> cast (U x)" - argument types are: (double)
Does anyone know why this happens?