#Differences between operator <=> defined in and outside a struct

23 messages · Page 1 of 1 (latest)

slate kestrel
#
#include <compare>

struct X {
    int a;

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

// Doesn't work 
static auto operator<=>(X const& lhs, X const& rhs) {
    return lhs.a <=> rhs.a;
}

int main(){
    return (X{0} <=> X{1}) == 0;
    return X{0} < X{1};
    return X{0} > X{1};
    return X{0} >= X{1};
    return X{0} <= X{1};
    return X{0} == X{1};
// <source>:20:17: error: invalid operands to binary expression ('X' and 'X')
//    20 |     return X{0} == X{1};
//       |            ~~~~ ^  ~~~~
}
blissful islandBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

surreal nexus
#

It's not about inside v. Outside, but defaulted vs. Not defaulted

#

If <=> is not defaulted, then it does not define ==

slate kestrel
surreal nexus
#

Yes

slate kestrel
# surreal nexus Yes

That's sad, I thought that the point of the <=> operator was to no have to define all the < > = <= >= != and just have to define a single one

surreal nexus
#

I assume it is because Issues arise on wether or not equivalent should result in true or false for ==

fast prism
#

i'd guess that the reasoning is that if you define a custom <=>, there's no reasonable way to know which of the other ones are meaningful or desired.

#

but dunno

#

might also be some compatibility issues

#

i'm sure the paper has a long exposition on the matter 😛

surreal nexus
#

Personally I feel like if <=> returns a strong ordering, then == should be defined

fast prism
#

i remember endless debates about these sorts of things

#

iirc they spent literally years debating this stuff

#

i'm sure they'll have had reasons

slate kestrel
#

!solved

blissful islandBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

slate kestrel
#

Thank you guys :)

fast prism
#

nice

#

thx for the link