Почему использование «префиксной» нотации оператора- приводит к сбою разрешения перегрузкиC++

Программы на C++. Форум разработчиков
Ответить
Гость
 Почему использование «префиксной» нотации оператора- приводит к сбою разрешения перегрузки

Сообщение Гость »


The following code snippet fails to compile:

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

struct HasOperatorMinus {     int v;     HasOperatorMinus operator-(HasOperatorMinus o) { return operator-(this->v, o.v); } }; auto f(HasOperatorMinus a) {     return a - a; } 
https://godbolt.org/z/Kx1Kccfzs says

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

:3:80: error: too many arguments to function call, expected single argument 'o', have 2 arguments     3 |     HasOperatorMinus operator-(HasOperatorMinus o) { return operator-(this->v, o.v); }       |                                                             ~~~~~~~~~          ^~~ :3:22: note: 'operator-' declared here     3 |     HasOperatorMinus operator-(HasOperatorMinus o) { return operator-(this->v, o.v); }       |                      ^         ~~~~~~~~~~~~~~~~~~ 
What the compiler says is that in the implementation

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

operator-(this->v, o.v)
, overload resolution failed. I do not understand why the binary overload of

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

operator-
for integers is hidden in this scenario.

Dear reader, please do not tell us that we can use the "infix" notation in the implementation. The question is about the difference between the prefix notation and the infix.


Источник: https://stackoverflow.com/questions/781 ... ion-failur
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

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