C++ Logo

std-discussion

Advanced search

Re: The std::equality_comparable concept and reflexivity.

From: Jan Schultke <janschultke_at_[hidden]>
Date: Wed, 30 Jul 2025 07:51:26 +0200
Correction: I've misremembered something.

x != x

is true for NaN, so NaN seems to be supported indeed, and that's
possibly the motivation.

On Wed, 30 Jul 2025 at 07:48, Jan Schultke <janschultke_at_[hidden]> wrote:
>
> > This got me wondering whether reflexivity
> > is a semantic requirement of std::equality_comparable or not?
>
> My reading is that it's not a requirement.
> To model weakly-equality-comparable-with<T,U>,
> with lvalues t and u of types T and U,
> the following requirement has to be met:
>
> > bool(t != u) == !bool(t == u).
>
> See https://eel.is/c++draft/concept.equalitycomparable
>
> For a value such as NaN, this is "false == true" because all
> comparisons are false. If a value was equal to all other values but
> not to itself, then equality wouldn't be transitive, so that also
> wouldn't work. HOWEVER, if == always yields false and != always yields
> true, then it seems like all the requirements are met.
>
> I don't know what the motivation is, seeing that NaN is not supported,
> but I suppose it's a slightly more minimalistic requirement if we
> allow values that are equal to nothing, not even themselves, even
> though I cannot think of a good use case.

Received on 2025-07-30 05:51:45