考虑以下代码:
struct A {
void operator++() const {}
};
void operator++(const A&) {}
int main () {
const A ca;
++ca; // g++ Error (as expected): ambiguous overload for ‘operator++’
A a;
++a; // g++ Warning: "ISO C++ says that these are ambiguous,
// even though the worst conversion for the first is better
// than the worst conversion for the second"
// candidate 1: void operator++(const A&)
// candidate 2: void A::operator++() const
}
为什么g++只发出警告而不是错误提示
++a
?换句话说,非成员函数如何比成员函数更适合?谢谢!
-pedantic-errors
,它应该会报错。 - Collin Dauphinee