C++ Logo

std-discussion

Advanced search

Comparison operator rewriting, again

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Fri, 14 Jul 2023 02:49:17 +0300
Hi.

Recently I got bitten by this innocent piece of code:

struct comparible_UDT
{
   int i_;
   comparible_UDT() : i_(2){}
   bool operator == (const comparible_UDT& v) { return v.i_ == i_; }
};

bool test(comparible_UDT left, comparible_UDT right)
{
    return left == right;
}

This used to work fine until C++20, where the completely broken piece
of... core language called operator rewriting appeared. Now gcc and
clang mercifully accept this code, albeit with a warning, and MSVC fails
to compile it saying the operator== is ambiguous with its rewritten
self. Which, of course, I did not write and cannot suppress.

https://godbolt.org/z/hsfE8sYfG

(Yes, I know I can fix this by marking the operator== as const, that's
not the point. The point is that the code above should work as written,
there's nothing ambiguous about it.)

Could someone explain the rationale behind making this code invalid in
C++20? Is there a rationale at all?

If there isn't, is this a bug? Should it be reported as a DR?

Thanks.

Received on 2023-07-13 23:49:22