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

            Bug ID: 118736
           Summary: Requesting a diagnostic on infinite recursion with
                    polymorphic, defaulted operator==
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Here's a reduction of a problem we ran into:

struct Base {
  virtual auto operator==(Base const&) const -> bool = 0;
};

struct Derived : Base {
  int m;

  auto operator==(Base const& rhs) const -> bool override {
    if (auto p = dynamic_cast<Derived const*>(&rhs)) {
      return *this == *p;
    } else {
      return false;
    }
  }

  auto operator==(Derived const&) const -> bool = default; // BAD!
};

In reality, Derived has multiple members, so it's nice to be able to just
default the comparison since we want to compare all the members. However,
defaulting Derived's equality operator here doesn't just give us all the
member-wise comparisons — it also gives us all the base subobject comparisons.
Which includes comparing the Base (abstract) subobject, whose comparison
recursively re-invokes the Derived comparison. This is infinite recursion. 

It would be really nice to get a warning here. I'm not sure how exactly to even
specify this warning, but the above code is definitely bad and gcc does not
currently warn on it (nor does clang).
  • [Bug c++/118736] New: Requestin... barry.revzin at gmail dot com via Gcc-bugs

Reply via email to