C++ Logo

std-discussion

Advanced search

Re: Pointer comparison with operators vs function objects

From: Jennifier Burnett <jenni_at_[hidden]>
Date: Mon, 15 Sep 2025 19:19:34 +0100
As I understood it, the reason was because of segmented architectures where pointers are stored as segment id + offset, and thus comparison using < would only require comparing the offsets, whereas std::less would require comparing both the offset and segment id to ensure that for two pointers with the same offset A < B == B >= A.

Under such a scheme making < and std::less equivalent would mean introducing a redundant check when comparing pointers with the same array (which I imagine is usually the most common case for using < on two pointers) and potentially inducing a performance penalty if the segment id for one of the pointers is in some processor state register and needs to be moved to a gpr first.

On 15 September 2025 13:48:52 BST, Yongwei Wu via Std-Discussion <std-discussion_at_[hidden]> 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?
>
>--
>Yongwei Wu
>URL: http://wyw.dcweb.cn/

Received on 2025-09-15 18:19:41