79620444

Date: 2025-05-13 21:30:43
Score: 0.5
Natty:
Report link

When you use:


auto operator<=>(const X&) const = default;

the compiler automatically synthesizes operator== for you, because it knows your intent to use defaulted comparisons for this type.

However, when you define:

auto operator<=>(const X& other) const { return Dummy <=> other.Dummy; }

you’re providing a custom implementation, and the compiler no longer assumes what equality should mean, so it does not generate operator==.

If you still want == support with a custom <=>, you must define it manually, like this:

#include <compare>

struct X {
    int Dummy = 0;

    auto operator<=>(const X& other) const {
        return Dummy <=> other.Dummy;
    }

    bool operator==(const X& other) const {
        return (*this <=> other) == 0;
    }
};

This tells the compiler explicitly what equality means based on your comparison logic.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you use
  • Low reputation (1):
Posted by: Gildos