我认为这个例子最能说明我的问题:
namespace N {
class C {
public:
friend bool operator==(const C& c, const C& x) {
return true;
}
friend bool f(const C& c, const C& x) {
return true;
}
};
class D {
public:
bool operator==(const D& x) {
bool a = C{} == C{}; // this works
return true;
}
bool f(const D& x) {
bool a = f(C{}, C{}); // this does not work
return true;
}
};
}
我一直认为重载运算符就像函数一样,除了“调用语法”,如果你愿意的话。我只是在 ADL 或名称查找规则中注意到了上述差异(我不知道是哪一个)。
有人可以解释为什么bool operator==(const C& c, const C& x)
找到但bool f(const C& c, const C& x)
不是吗?