We need to explicitly skip over vptr fields when synthesizing a
defaulted comparison operator, because next_initializable_field
doesn't do so for us.
PR c++/95567
gcc/cp/ChangeLog:
* method.c (build_comparison_op): Skip DECL_VIRTUAL_P fields.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/spaceship-virtual1.C: New test.
field;
field = next_initializable_field (DECL_CHAIN (field)))
{
+ if (DECL_VIRTUAL_P (field))
+ /* Don't compare vptr fields. */
+ continue;
+
tree expr_type = TREE_TYPE (field);
location_t field_loc = DECL_SOURCE_LOCATION (field);
--- /dev/null
+// PR c++/95567
+// { dg-do run { target c++20 } }
+
+struct B {
+ B(int i) : i(i) {}
+ virtual ~B() = default;
+
+ bool operator==(B const&) const = default;
+ int i;
+};
+
+struct D : B {
+ D(int i, int j) : B(i), j(j) {}
+ int j;
+};
+
+int main() {
+ if (B(2) != D(2, 3))
+ __builtin_abort();
+}