C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Standard attribute [[nocompare]] for defaulted comparisons

From: Avi Kivity <avi_at_[hidden]>
Date: Sun, 05 Feb 2023 14:42:20 +0200
How about:

struct s {
    int id;
    std::string name;

    auto operator<=>(const s& x) const { return for_compare() <=>
x.for_compare(); }
private:
    auto for_compare() const { return std::forward_as_tuple(id); }
};

This can be used to omit certain fields, change the ordering, negate a
field to reverse to comparison sense, etc.

On Mon, 2023-01-16 at 21:26 +0000, joeri _ via Std-Proposals wrote:
> The introduction of a standard attribute [[nocompare]] which can be
> used to indicate that a suboject of T should be omitted from a
> defaulted comparison.
>
> struct s {
> int id;
> [[nocompare]] std::string name;
>
> auto operator<=>(s const&) const = default;
> };
>
> Only member 'id' of class 's' will be compared when ordering or
> testing objects of 's' for equality. This prevents having to define
> all the relational operators when only wanting to opt-out a single
> data-member, and it mitigates the user of having to write dry and
> error-prone code.
>
> With kind regards,
>
> Joeri Kok


Received on 2023-02-05 12:42:08