Consider:
struct Y {
Y(float f) : f(f) {}
float f;
};
struct X {
X(Y y) : i(y.f) {}
int i;
friend bool operator==(X x1, X x2) {
return x1.i == x2.i;
}
};
int main()
{
return Y(1) == Y(2); // ERROR
}
This causes the following error on MSVC and a similar one on Clang:
'==': candidate function(s) not accessible
could be the friend function at '..\main.cpp(11)' : '==' [may be found via argument-dependent lookup]
If I move the definition of the friend function out of the class:
struct X {
X(Y y) : i(y.f) {}
int i;
friend bool operator==(X x1, X x2);
};
inline bool operator==(X x1, X x2)
{
return x1.i == x2.i;
}
The expression in main()
above compiles fine.
Is this mandated by the standard or a bug? If it's mandated: why?