C++ Logo

std-proposals

Advanced search

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

From: joeri _ <joeri_k_at_[hidden]>
Date: Mon, 16 Jan 2023 21:26:39 +0000
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-01-16 21:26:41