Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::less<T *>

24 views
Skip to first unread message

Bonita Montero

unread,
Jan 14, 2020, 2:12:10 AM1/14/20
to
I cppreference I found:
"A specialization of std::less for any pointer type yields a
strict total order, even if the built-in operator < does not. ..."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Why should the < operator not gain a "strict total order" on
pointers?

Bonita Montero

unread,
Jan 14, 2020, 2:34:02 AM1/14/20
to
Even segmented addressed pointers had a "strict total order".

Paavo Helde

unread,
Jan 14, 2020, 5:14:41 AM1/14/20
to
This is the zero overhead principle. Normalizing the segmented pointers
for strict total order comparison would take some CPU cycles and use
more memory. However, the whole point of segmented architectures is to
avoid this overhead when working inside a single segment. C and C++
standards have been carefully worded to support such architectures, so
that fast comparison can be used inside a single array where it matters
most.

A hypothetical C++ compiler supporting "strict total order" for '<'
operator would have to emit a more expensive comparison for '<' every
time it could not prove the pointers point to the same array, making the
same code run slower than in C. This was and is a big no-no, especially
in the past when optimizers were weak and segmented architectures were a
real thing.

Bonita Montero

unread,
Jan 14, 2020, 8:22:46 AM1/14/20
to
> A hypothetical C++ compiler supporting "strict total order" for '<'
> operator would have to emit a more expensive comparison for '<' every
> time it could not prove the pointers point to the same array, ...

That's not hypothetical but true for the ancient 808x- / 80286-compilers
depending on the chosen memory-model. Even the early MS Quick C compiler
has full blown pointer arithmetics for every pointer if you chose com-
pact, large or huge memory model. And even if you chose the small or
medium memory-model with one data-segment you usually address memory
outside the data-segment with pointers attributed as far or huge
which gives you the same comparison-semantics. And with huge you might
even address arrays spanning more than one segment if the data type
fits an even numbered times in a segment.
And most of that isn't specified in the language-standard but is com-
piler and platforms-specific.
0 new messages