The following code:
struct X {
X() {}
};
struct Y {
Y() {}
Y(X) {}
Y(int) {}
friend bool operator==(const Y&, const Y&) { return false; }
};
bool f()
{
return 1 == X();
}
fails to compile with the following error:
error: no match for 'operator==' (operand types are 'int' and 'X')
return 1 == X();
While if I move definition of operator==
outside of the class it works just fine:
struct X {
X() {}
};
struct Y {
Y() {}
Y(X) {}
Y(int) {}
friend bool operator==(const Y&, const Y&);
};
inline bool operator==(const Y&, const Y&) { return false; }
bool f()
{
return 1 == X();
}
Could someone explain why? (Ideally, with some quote from the standard and human-readable explanation/motivation.) In the answer here: https://stackoverflow.com/a/20114792/1350936 @rightfold mentioned that
Functions defined outside of the class can be found even without ADL
But I don't quite understand what it means.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…