Skip to content

C++20 operator== ambiguous with derived-to-base conversion #53954

Closed
@d0k

Description

@d0k
struct P {
#if 1
    template<class S>
    bool operator==(const S &) const;
#else
    template<class S>
    friend bool operator==(const P &, const S &);
#endif
};

struct A : public P {};
struct B : public P {};

bool check(A a, B b) {
    return a == b;
}

The friend function works just fine, but the member function dies with

<source>:15:14: error: use of overloaded operator '==' is ambiguous (with operand types 'A' and 'B')
    return a == b;
           ~ ^  ~
<source>:4:10: note: candidate function [with S = B]
    bool operator==(const S &) const;
         ^
<source>:4:10: note: candidate function (with reversed parameter order) [with S = A]

GCC and MSVC accept both. https://godbolt.org/z/9hj9554fn

@dwblaikie @zygoloid

Metadata

Metadata

Assignees

Labels

c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions