Code: Select all
template
struct base_widget {
bool operator==(T const&) const;
};
struct gadget : base_widget {};
bool foo(gadget const& a, gadget const& b) {
return a == b;
}
< /code>
Für MSVC ist dies ein Fehler: < /p>
error C2666: 'base_widget::operator ==': overloaded functions have similar conversions
could be 'bool base_widget::operator ==(const T &) const'
with
[
T=gadget
]
or 'bool base_widget::operator ==(const T &) const' [synthesized expression 'y == x']
with
[
T=gadget
]
while trying to match the argument list '(const gadget, const gadget)'
< /code>
Für GCC ist es eine Warnung: < /p>
In function 'bool foo(const gadget&, const gadget&)':
warning: C++20 says that these are ambiguous, even though the second is reversed:
| return a == b;
| ^
note: candidate 1: 'bool base_widget::operator==(const T&) const [with T = gadget]'
| bool operator==(T const&) const;
| ^~~~~~~~
note: candidate 2: 'bool base_widget::operator==(const T&) const [with T = gadget]' (reversed)
< /code>
und auch eine Warnung für Klang: < /p>
warning: ISO C++20 considers use of overloaded operator '==' (with operand types 'const gadget' and 'const gadget') to be ambiguous despite there being a unique best viable function [-Wambiguous-reversed-operator]
| return a == b;
| ~ ^ ~
note: ambiguity is between a regular call to this operator and a call with the argument order reversed
| bool operator==(T const&) const;
|
Überlastauflösung/Bediener in Ausdrücken < BR />
-Aover.match.oper weibliche; ,
Für jeden nicht verwirrten Kandidaten für den Ausdruck y == x.
Erstellen des Operators
Code: Select all
template
struct base_widget {
friend bool operator==(T const&, T const&);
};