Home > OS >  Compilation error when using Parent::operator<=> in two derived classes only on gcc 12 but not
Compilation error when using Parent::operator<=> in two derived classes only on gcc 12 but not

Time:02-04

Consider the following code main.cpp:

#include<compare>
struct Parent {
    int val;
    auto operator<=>(const Parent&) const = default;
};

struct Child1: public Parent {
    using Parent::operator<=>;
};

struct Child2: public Parent {
    using Parent::operator<=>;
};
int main()
{
}

It compiles for clang -14 -std=c 20 main.cpp but not for g -12 -std=c 20 main.cpp:

main.cpp:4:10: error: comparison operator ‘bool Parent::operator==(const Parent&) const’ defaulted after its first declaration
    4 |     auto operator<=>(const Parent&) const = default;
      |          ^~~~~~~~
main.cpp:4:10: note: ‘bool Parent::operator==(const Parent&) const’ previously declared here

Why?

CodePudding user response:

Looks like a bug in GCC which you could file here: https://gcc.gnu.org/bugzilla/enter_bug.cgi?product=gcc

Here is a simple workaround:

struct Parent {
    int val;
    auto operator<=>(const Parent& h2) const;
};

struct Child1: public Parent {
    using Parent::operator<=>;
};

struct Child2: public Parent {
    using Parent::operator<=>;
};

inline auto Parent::operator<=>(const Parent& h2) const = default;
  • Related