https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94061

--- Comment #4 from Baudouin Feildel <feildel+gccbugzi...@corona-renderer.com> 
---
We also ran into this issue with operator==(). This behavior might be explained
by some implementation detail of the generated code for the operator, but we
believe it is wrong because it is inconsistent, see the following example
(https://godbolt.org/z/f4EjojExh):

struct Base {
  protected:
    Base() = default;
    bool operator==(const Base& other) const = default;
};

struct Child : Base {
    int         i = 0;
    Child() = default;
    bool operator==(const Child& other) const = default;
};

int main() 
{
    Child a1;
    Child b1;
    return a1 == b1;
}

Here calling protected constructor in defaulted ctor is totally fine. But
trying to do the same with comparison operator fails with the following
compiler error:

<source>: In function 'int main()':
<source>:17:18: error: use of deleted function 'constexpr bool
Child::operator==(const Child&) const'
   17 |     return a1 == b1;
      |                  ^~
<source>:10:10: note: 'constexpr bool Child::operator==(const Child&) const' is
implicitly deleted because the default definition would be ill-formed:
   10 |     bool operator==(const Child& other) const = default;
      |          ^~~~~~~~
<source>:10:10: error: 'bool Base::operator==(const Base&) const' is protected
within this context
<source>:4:10: note: declared protected here
    4 |     bool operator==(const Base& other) const = default;
      |          ^~~~~~~~




This behavior is unfortunate and make it very difficult to use defaulted
operator== when you have a common base class that should not be comparable. See
this other example: https://godbolt.org/z/v78r1nYr9.

Reply via email to