C++ Logo

std-discussion

Advanced search

Re: Pointer comparison with operators vs function objects

From: Nate Eldredge <nate_at_[hidden]>
Date: Tue, 16 Sep 2025 05:54:04 +0000
On Sep 15, 2025, at 23:44, Nate Eldredge via Std-Discussion <std-discussion_at_[hidden]> wrote:
>
>> On Sep 15, 2025, at 19:25, Yongwei Wu via Std-Discussion <std-discussion_at_[hidden]> wrote:
>>
>> Another angle: Can we make operator< on pointers
>> implementation-defined, as more often than not it has quite
>> well-defined behaviour? UB is a dangerous thing, and it is better to
>> have as little UB as possible. (I also hate the fact that sometimes
>> compilers utilize UB as a crazy licence to kill, and I do not want
>> anything like that for something as simple as p1 < p2.)
>
> It's not UB, is it? Rather, the result is unspecified: https://eel.is/c++draft/expr.rel#4 and https://eel.is/c++draft/expr.rel#5. If `p1` and `p2` don't point within the same array, then `p1 < p2` may yield true or it may yield false, but it can't impregnate your pets or summon nasal demons. It need not be consistent with any total or partial order, so `p1 < p2 && p2 < p1` could be true, as could `p1 < p2 && p2 < p3 && p3 < p1`. But there's no "license to kill".

Perhaps you're thinking back to C++98, in which it was indeed UB to evaluate `p1 < p2` for unrelated pointers. But it changed to "unspecified" for C++03, I believe, and has been so ever since. Or perhaps you're thinking of C, in which it remains UB as of C23.

Received on 2025-09-16 05:54:09