Оператор == Неоднозначность в C ++ 20C++

Программы на C++. Форум разработчиков
Anonymous
Оператор == Неоднозначность в C ++ 20

Сообщение Anonymous »

Почему этот код неоднозначный начинается с C ++ 20? < /p>

Код: Выделить всё

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>
Для MSVC это ошибка: < /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>
для GCC это предупреждение: < /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>
, а также предупреждение для Clang: < /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;
|
Я знаю, что C ++ 20 допускает это:

Разрешение/операторы перегрузки в экспрессии < Br /> over.match.operí
Для операторов равенства переписанные кандидаты также включают в себя
синтезированный кандидат, с порядок двух параметров, обратный ,
для каждого неворотого кандидата для выражения y == x. < /p>
< /blockquote>
Но почему синтезированный обратный оператор проблематичен в этом случае?
Создание оператора == Друг Исправляет проблему:

Код: Выделить всё

template 
struct base_widget {
friend bool operator==(T const&, T const&);
};
Почему в этом случае нет проблем с реверсией?

Подробнее здесь: https://stackoverflow.com/questions/794 ... ity-in-c20

Вернуться в «C++»