Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++20 comparison: warning about ambiguous reversed operator

Tags:

c++

c++20

Consider this valid C++17 example:

struct A {
   bool operator==(const A&);
};


int main() {
   return A{} == A{};
}

When compiled in clang with -std=c++20 it gives:

<source>:7:15: warning: ISO C++20 considers use of overloaded operator '==' (with operand types 'A' and 'A') to be ambiguous despite there being a unique best viable function [-Wambiguous-reversed-operator]

   return A{} == A{};

          ~~~ ^  ~~~

<source>:2:9: note: ambiguity is between a regular call to this operator and a call with the argument order reversed

   bool operator==(const A&);

Does this warning mean that C++20 disallows using a typical comparison operator to compare two objects of the same type? What is the correct alternative? Is the situation expected to change in future drafts?

like image 798
Lack Avatar asked Feb 25 '20 02:02

Lack


2 Answers

Does this warning mean that C++20 disallows using a typical comparison operator to compare two objects of the same type? What is the correct alternative? Is the situation expected to change in future drafts?

This isn't really a typical comparison operator, it's already kind of wrong - since it only allows a const object on one side (your type A wouldn't satisfy the new equality_comparable concept either, even without any langauge changes).

You have to write it this way:

struct A {
   bool operator==(const A&) const;
//                          ^^^^^^
};

This is the final rule for C++20.


The specific issue is that in C++20, comparison operators add a new notion of rewritten and reversed candidates. So lookup for the expression a == b will also end up matching operators like b == a. In the typical case, this means you have to write fewer operators, since we know equality is commutative.

But if you have a const-mismatch, what happens is you end up with these two candidates:

bool operator==(/* this*/ A&, A const&); // member function
bool operator==(A const&, /* this*/ A&); // reversed member function

With two arguments of type A. The first candidate is better in the first argument, and the second candidate is better in the second argument. Neither candidate is better than the other, hence ambiguous.

like image 63
Barry Avatar answered Oct 17 '22 23:10

Barry


It is a general rule of overload resolution that each argument type must be separately at least as close to the parameter type for a selected function as to the parameter type for any other:

struct A {A(int);};
void f(long,int);   // #1
void f(int,A);      // #2
void g() {f(0,0);}  // error: ambiguous

The much worse conversion for the second argument for #2 doesn’t make up for the intlong conversion on the first argument.

In C++20, various rewrite rules have been added to obviate the need to write so many all-but-identical comparison operator overloads. While the trivial ambiguities between hand-written “reversed candidates” and identical compiler-generated ones are handled by tie-breaker rules that prefer real functions, that’s (again) not enough to make up for a worse conversion for any argument.

Comparison operators written carefully according to accepted (C++17) practices will very rarely run afoul of this, but questionable signatures like this (with asymmetric const) may very well be problematic (in new ways). Hopefully more bugs are found than are caused by this extension.

like image 39
Davis Herring Avatar answered Oct 18 '22 01:10

Davis Herring