I have a program that looks like this:
class B {};
class A
{
template<typename T> int operator+(const T&) const { return 1; } // Function 1
};
template<typename T, typename P> int operator+(const T&, const P&) { return 2; } // Function 2
int main()
{
A a;
B b;
std::cout<<(a + b);
return 0;
}
In this scenario Function 2 will be called. But when I modify function 1 to be a free function instead of member function
template<typename T> int operator+(const A&, const T&) { return 1; } // Function 1
template<typename T, typename P> int operator+(const T&, const P&) { return 2; } // Function 2
Now Function 1 will be called, even though in both cases Function 1 is basically identical. What's the reasoning behind this? GCC complains about ambiguity according to ISO C++ standard but doesn't state the exact cause and compiles fine anyways. This case is unique to operators since they can be called in same way regardless if they are members or not.