0

In the below program, there are two structs A and B, each with its own method f and comparison operator ==. And the struct C inherits both A and B together with their methods. Then the program calls method and operator of an object of type C:

struct A { 
    bool f(const A &) const;
    bool operator ==(const A &) const; 
};
struct B { 
    bool f(const B &) const;
    bool operator ==(const B &) const; 
};
struct C : A, B {};

int main() { 
    B b;
    C c;
    //c.f(b); // error everywhere
    //c.operator ==(b); //error everywhere
    (void)(c == b); //error in GCC, ok in Clang and MSVC
}

The call to method f fails due to ambiguity as explained in the question Why do multiple-inherited functions with same name but different signatures not get treated as overloaded functions? . The same ambiguity error happens with explicit call to operator ==.

But the expression c == b is rejected only by GCC, while both Clang and MSVC accept it. Demo: https://gcc.godbolt.org/z/nhM1MzTM4

Which compiler behavior is right here?

Fedor
  • 13,764
  • 2
  • 28
  • 94

0 Answers0