Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit operator != resolving ambiguity in operators ==

If some struct A has explicitly declared operator == method, but at the same time there is a global operator == accepting arguments of type A, then equality comparison shall result in ambiguity error.

In C++20 we can call inequality operator !=, which the compiler will interpret as the negation of equality operator. I though that in the same circumstances ambiguity error shall appear as well, and indeed GCC and Clang show it, but the latest Visual Studio 2019 accepts the code:

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

bool operator ==(const A&, const A&) { return true; }

int main() {
    A a;
    //a == a; //error everywhere
    return a != a; //ok in MSVC
}

Demo: https://gcc.godbolt.org/z/ds53Wv783

Is it simply a bug in MSVC?

like image 584
Fedor Avatar asked Sep 21 '21 08:09

Fedor


1 Answers

It appears to be a bug in MSVC, which is expected to be fixed soon: https://developercommunity.visualstudio.com/t/incorrect-acceptable-of-ambiguous-operator/1535876

like image 63
Fedor Avatar answered Dec 15 '22 00:12

Fedor