C++ Logo

std-discussion

Advanced search

Re: Pointer comparison with operators vs function objects

From: Yongwei Wu <wuyongwei_at_[hidden]>
Date: Wed, 17 Sep 2025 08:59:18 +0800
On Tue, 16 Sept 2025 at 13:44, Nate Eldredge <nate_at_[hidden]> wrote:
> 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.

On Tue, 16 Sept 2025 at 16:56, Bo Persson via Std-Discussion
<std-discussion_at_[hidden]> wrote:
> > Very simple, it is far too easy to err on the side of UB, as most
> > (yes, I believe, most) C++ programmers do not know that one should NOT
> > compare arbitrary pointers with operator<.
> >
>
> But it is not UB, but "merely" unspecified. A function (or operator)
> that returns a bool will return either true or false. However, not very
> useful when we don't know which one.
>
> The standard just says: "Otherwise, neither pointer is required to
> compare greater than the other."
>
> https://eel.is/c++draft/expr.rel#4.3

On Wed, 17 Sept 2025 at 05:14, Julien Villemure-Fréchette via
Std-Discussion <std-discussion_at_[hidden]> wrote:
> The behaviour is actually unspecified behavior.

Big thanks to all of you! I learnt something, and it was a small
comfort that there was no licence to kill here. ;-)

-- 
Yongwei Wu
URL: http://wyw.dcweb.cn/

Received on 2025-09-17 00:59:36