Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading