c++gccclangspaceship-operator

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


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?


Solution

  • 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;