Date: Tue, 16 Sep 2025 10:56:14 +0200
On 2025-09-16 at 03:25, Yongwei Wu via Std-Discussion wrote:
> I'll reply to this answer to add more details about my question.
>
> On Mon, 15 Sept 2025 at 21:48, David Brown via Std-Discussion
> <std-discussion_at_[hidden]> wrote:
>>
>> On 15/09/2025 14:48, Yongwei Wu via Std-Discussion wrote:
>>> As per my understanding of the C++ standard, operators like < should not
>>> be used to compare unrelated pointers, but function objects like
>>> std::less can. Is there any reason why we cannot make p1 < p2 just
>>> behave like std::less<T*>{}(p1, p2)? I really cannot think any.
>>>
>>> Any insights on this?
>>>
>>
>> First ask yourself, is there any reason why you would want to compare
>> unrelated pointers for ordering? If there are no real-world use-cases
>> that can't be handled well enough using the existing C++ language, then
>> there is no reason to change.
>
> 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
> I'll reply to this answer to add more details about my question.
>
> On Mon, 15 Sept 2025 at 21:48, David Brown via Std-Discussion
> <std-discussion_at_[hidden]> wrote:
>>
>> On 15/09/2025 14:48, Yongwei Wu via Std-Discussion wrote:
>>> As per my understanding of the C++ standard, operators like < should not
>>> be used to compare unrelated pointers, but function objects like
>>> std::less can. Is there any reason why we cannot make p1 < p2 just
>>> behave like std::less<T*>{}(p1, p2)? I really cannot think any.
>>>
>>> Any insights on this?
>>>
>>
>> First ask yourself, is there any reason why you would want to compare
>> unrelated pointers for ordering? If there are no real-world use-cases
>> that can't be handled well enough using the existing C++ language, then
>> there is no reason to change.
>
> 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
Received on 2025-09-16 08:56:22