C++ Logo

sg14

Advanced search

Re: Colony/hive & unordered vs ordered == operator

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Mon, 14 Mar 2022 21:40:42 -0400
On Mon, Mar 14, 2022 at 8:20 PM Matt Bentley via SG14 <sg14_at_[hidden]>
wrote:

> Hi Everyone,
> quick question:
> colony/hive is unordered in terms of insertion position (but sortable),
> should the ==/!= operator also be unordered ie.
> should hive<int> {1,2, 3} == hive<int> {3, 1, 2}?
>
> My main interest is not which is more 'correct' but which is more useful
> in practice?
> I don't use == operator for containers a lot so I don't know.
> Be aware this will affect >= < etc also.
>

Another option to be considered is: you can provide *no* operator== at all.
Compare to std::unordered_set<T>, which provides an *order-insensitive*
operator== and *no* operator<=>.
Compare to sg14::slot_map<T>, which provides *no* operator==.

You need to decide whether order is *salient*. (Salient properties, such as
vector::size, are compared by operator== and copied by the copy ctor.
Non-salient properties, such as vector::capacity, are not.)

One thing to consider is that copies are equal by definition.
    std::hive<T> a = { ... };
    std::hive<T> b = a;
    assert(a == b); // if operator== is provided at all, then this *must*
be true
You mustn't ever provide an operator== that breaks this invariant. Maybe
that greatly simplifies your decision, maybe it doesn't, I don't know.

HTH,
Arthur

Received on 2022-03-15 01:40:55