I was playing around with function templates and I stumbled across a weird interaction.
template<class T1, class T2>
void foo(T1, T1);
template<class T1, class T2>
void foo(T1, T2);
//main
foo(1,1)
This calls foo(T1, T2) and I don't understand why. How would that work? Are these functions overloads of each other, and why would the compiler choose the one with different parameter types?
this particular interaction was explained in the first part of Henri Menke's post
After some more messing around I found something weirder
#include <iostream>
template<class T1, class T2>
void foo(T1 a, T1 b)
{
std::cout << "same\n";
}
template<class T1, class T2>
void foo(T1 a, T2 b)
{
std::cout << "different\n";
}
int main()
{
foo(1, 1);
foo<int, int>(1, 1);
}
In this code I get a result
different
different
but after commenting out the 1st call like
int main()
{
//foo(1, 1);
foo<int, int>(1, 1);
}
the result is
same
I'm using VS2015 and if I write the same thing in Ideone (like here) the result for the 1st one is
different
same
Could somebody be able to explain what is (or isn't) going on?
By the way I came to the conclusion that the call foo<int, int>(1, 1);
should be ambiguous. Both function templates have the same signature then and come from the same template. So that is another thing, why don't they collide?