I'm looking for a way to implement the three-way comparison operator and the operator== for the following class:
class Foo
{
public:
auto operator<=>( const Foo& rhs ) const noexcept = default;
private:
std::uint32_t m_Y;
std::uint32_t m_X;
char m_C;
std::vector<char> m_Vec;
};
But the default implementation is not what I intended. Therefore I need to write my own implementation.
What I want is:
- I want the equality comparison (
==and!=) to be based on comparing the membersm_Y,m_X, andm_Cof two operands (of typeFoo). All of those three members must be equal to satisfy the equality. The equality of the content ofm_Vecis not important (since it's not efficient to compare all those elements lexicographically). - I want the ordering comparison (
<and>) to be based on comparing the expressionm_Y*m_X. - I want the ordering comparison (
<=and>=) to be based on comparing the expressionm_Y*m_Xand if both operands are equal in that regard, then all three members of both operands should be equal to satisfy the<=or>=(just like in==).
Also with all this said, which comparison category type suits this scenario better, std::strong_ordering or std::weak_ordering?
How should I implement such logic? It seems simple and I read a whole recipe in a C++20 book on this topic and I still can't figure it out.
I believe this is a partial order, which is an order where elements may be incomparable (none of
<,>, or==hold between them). You should verify the necessary laws hold (a <= biffa < b || a == b,a <= afor alla,a == bifa <= b && b <= afor alla,b, anda <= cifa <= b && b <= cfor alla,b,c). If that is the case, usestd::partial_ordering.