There is something i don't quite understand in the example below.
When calling the function are_equal from the main, it is done with an int (10) and a float (10.0). Therefor the compiler will 'decide' that T means int and U means float. In the way i think the compare a==b could be translated in int(10)==float(10.0), and should be false. But it returns true: "x and y are equal\n"
I would understand if the template were: template <class T> and bool are_equal<T a, T b>, thereby converting the float to int...
Where do i go wrong in my logic?
Doesn't comparing two different types always ends up in false?
(or am i mixing up with javascript ===)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template <class T, class U>
bool are_equal (T a, U b)
{
return (a==b);
}
int main ()
{
if (are_equal(10,10.0))
cout << "x and y are equal\n";
else
cout << "x and y are not equal\n";
return 0;
}
Sorry, i figured it out. I may compare float with int. But which one is converted to the other in this case? Has float become int?
And why was i told that c/c++ isn't loosely typed?