Date: Wed, 17 Sep 2025 09:03:06 +0200
On 16/09/2025 23:58, Julien Villemure-Fréchette via Std-Discussion wrote:
>
> One important point to mention: the standard guarantee that the < relationship is a partial order, so even though the result of 'a < b' may be unspecified, if 'a < b' does evaluate to true then it follows that 'b <= a' evaluates to false.
>
I am looking at the C++20 standards at the moment (I just happen to have
that version open at the moment), so perhaps things have changed since
then. (I have also looked at cppreference.com - it is not the standard
itself, but is usually very precise.)
I don't see that the standard guarantees that < on pointers is a partial
order. The standard guarantees that a "strict total order over pointers
in the program" exists, but not that < and > on pointers follows it.
std::less<> and related templates stick to the total order. And you are
guaranteed that if two pointers can be compared with < to give a
specified result (they are in the same array or object), then you get
the same result from < and std::less<>.
Each comparison has unspecified results, evaluated independently. It is
even perfectly fine for the compiler to evaluate "(a < b) || (b >= a)"
as constantly false, and use that for reducing and simplifying the
generated code, as long as it can be entirely sure that the pointers are
not pointing within the same array (or one past it) or object.
>
> One important point to mention: the standard guarantee that the < relationship is a partial order, so even though the result of 'a < b' may be unspecified, if 'a < b' does evaluate to true then it follows that 'b <= a' evaluates to false.
>
I am looking at the C++20 standards at the moment (I just happen to have
that version open at the moment), so perhaps things have changed since
then. (I have also looked at cppreference.com - it is not the standard
itself, but is usually very precise.)
I don't see that the standard guarantees that < on pointers is a partial
order. The standard guarantees that a "strict total order over pointers
in the program" exists, but not that < and > on pointers follows it.
std::less<> and related templates stick to the total order. And you are
guaranteed that if two pointers can be compared with < to give a
specified result (they are in the same array or object), then you get
the same result from < and std::less<>.
Each comparison has unspecified results, evaluated independently. It is
even perfectly fine for the compiler to evaluate "(a < b) || (b >= a)"
as constantly false, and use that for reducing and simplifying the
generated code, as long as it can be entirely sure that the pointers are
not pointing within the same array (or one past it) or object.
Received on 2025-09-17 07:03:12