https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104785
Bug ID: 104785
Summary: Generated defaulted constexpr operator== produces
wrong code
Product: gcc
Version: 11.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: delleyves at gmx dot ch
Target Milestone: ---
It seems that the code generated for a defaulted constexpr `operator==` for a
struct that inherits a field whose type contains another such `operator==` does
not take that field into account. Removing constexpr on the top-most
`operator==` restores correct behaviour. Therefore, this is likely related to
#102490, although that one deals with bitfields. The behaviour seems to be
present on all versions of GCC supporting C++20's defaulted equality operators,
I did test 10.1 through 11.2 on godbolt.
In the following example (godbolt link: https://gcc.godbolt.org/z/7nE67fKxc),
the result is wrong only for the `D` type:
#include <iostream>
struct A
{
int a;
[[nodiscard]] constexpr bool operator==(const A&) const = default;
};
struct B
{
A a;
int b;
[[nodiscard]] constexpr bool operator==(const B&) const = default;
};
struct C : B
{
int c;
[[nodiscard]] bool operator==(const C&) const = default;
};
struct D : B
{
int d;
[[nodiscard]] constexpr bool operator==(const D&) const = default;
};
std::ostream &operator<<(std::ostream &os, const A &a) {
os << "A("<<a.a<<")";
return os;
}
std::ostream &operator<<(std::ostream &os, const B &b) {
os << "B("<<b.a<<", "<<b.b<<")";
return os;
}
std::ostream &operator<<(std::ostream &os, const C &c) {
os << "C("<<c.a<<", "<<c.b<<", "<<c.c<<")";
return os;
}
std::ostream &operator<<(std::ostream &os, const D &d) {
os << "D("<<d.a<<", "<<d.b<<", "<<d.d<<")";
return os;
}
int main()
{
A a1{1}, a2{2};
B b1{a1,2}, b2{a2,2};
C c1{b1,3}, c2{b2,3};
D d1{b1,3}, d2{b2,3};
#define CHECK(x,y) std::cout << x << " == " << y << "? " << ((x==y) ? "true" :
"false") << std::endl;
CHECK(a1,a2);
CHECK(b1,b2);
CHECK(c1,c2);
CHECK(d1,d2);
}