After a regression in GCC 13.3, I had to add constexpr
to defaulted spaceship operators in my program, and observed some discrepancies in how compilers treat some code in C++20 mode. These can be shown on equality comparison operator for the sake of code brevity below.
Example #1:
struct A {
operator int() const;
};
struct B : A {
constexpr bool operator==(const B &) const = default;
};
which is ok for Clang and MSVC, but GCC with -pedantic-errors
option complains here:
error: call to non-‘constexpr’ function ‘A::operator int() const’ [-Winvalid-constexpr]
Example #2:
struct A {
bool operator==(const A &) const;
};
struct B : A {
constexpr bool operator==(const B &) const = default;
};
which ok only for MSVC, while Clang with -pedantic-errors
option starts rejecting it as well with the
error: defaulted definition of equality comparison operator that is declared constexpr but invokes a non-constexpr comparison function is a C++23 extension [-Werror,-Wc++23-default-comp-relaxed-constexpr]
Are both code examples valid only in C++23 and not valid in C++20?