Por que esse código é ambíguo começando com C++20?
template <typename T>
struct base_widget {
bool operator==(T const&) const;
};
struct gadget : base_widget<gadget> {};
bool foo(gadget const& a, gadget const& b) {
return a == b;
}
para MSVC isso é um erro:
error C2666: 'base_widget<gadget>::operator ==': overloaded functions have similar conversions
could be 'bool base_widget<gadget>::operator ==(const T &) const'
with
[
T=gadget
]
or 'bool base_widget<gadget>::operator ==(const T &) const' [synthesized expression 'y == x']
with
[
T=gadget
]
while trying to match the argument list '(const gadget, const gadget)'
para o GCC é um aviso:
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<T>::operator==(const T&) const [with T = gadget]'
| bool operator==(T const&) const;
| ^~~~~~~~
note: candidate 2: 'bool base_widget<T>::operator==(const T&) const [with T = gadget]' (reversed)
E também um aviso para Clang:
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;
|
Eu sei que C++20 permite isso:
Resolução de sobrecarga/operadores em expressões
[sobre.correspondência.oper]
Para os operadores de igualdade, os candidatos reescritos também incluem um candidato sintetizado, com a ordem dos dois parâmetros invertida, para cada candidato não reescrito para a expressão y == x.
Mas por que o operador reverso sintetizado é problemático neste caso?
EDIT: Fazer o operator==
a friend
corrige o problema:
template <typename T>
struct base_widget {
friend bool operator==(T const&, T const&);
};
Por que não há problema em reverter neste caso?