Given the following code:
#include <iostream>
struct Alice
{
template <typename A>
void operator|(const A& /*a*/) const
{
std::cout << "operator| member" << std::endl;
}
};
template <typename A>
void operator|(const A& /*a*/, const Alice& /*alice*/)
{
std::cout << "operator| non-member" << std::endl;
}
int main()
{
Alice a;
Alice b;
a | b;
return 0;
}
It compiles without warning with both GCC 4.8.1, 4.9 and clang 3.4, but gives different results.
$ g++ -Wall -Wextra -std=c++11 alice.cpp && ./a.out
operator| non-member
$ clang++ -Wall -Wextra -std=c++11 alice.cpp && ./a.out
operator| member
What causes this difference? How can I force the same behaviour?
EDIT: Interesting fact: removing the const
qualifier from the member function makes gcc prefer the member function also. It does not solve the problem, however.
EDIT: clang++ prefers the non-member instead, if -std=c++11
is not specified.
EDIT: ICC 14.0 prefers non-member, no warning emitted.