Ambiguous Overloaded Operator C++20

I’m trying to test my project in the latest Visual Studio and Clang versions. One of the errors that pops up is related to an ambiguous operator (with reversed parameter order). This does not seem to pop up in C++17.

For example: (https://godbolt.org/z/Gazbbo)

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

struct B : private A {
    B(const A&);
    bool operator==(const B& other) const { return false; }
};

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

I’m unsure why this would be an issue. It seems to me that the only viable function here is bool operator==(const B& other) const as A can be implicitly converted to B but not the reverse. Indeed, if I mark B(const A&) with explicit I instead get an error that B can not be converted to the private base of A.

I’m trying to understand what I can do to avoid this, aside from using explicit or using B(a). Imagine A and B were library code, how do I support C++20 without breaking my interface in lower versions?

Source: Windows Questions C++

LEAVE A COMMENT