C++ Logo


Advanced search

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

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Sun, 05 Feb 2023 13:00:16 +0000

On 5 February 2023 12:42:20 GMT, Avi Kivity via Std-Proposals <std-proposals_at_[hidden]> wrote:
>How about:
>struct s {
> int id;
> std::string name;
> auto operator<=>(const s& x) const { return for_compare() <=>
>x.for_compare(); }
> 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.

I like this pattern, usually I call the function "key", instead of "for_compare". You probably also want to define operator== similarly as well once <=> is not defaulted.


Received on 2023-02-05 13:00:22