How can pointers be totally ordered?

前端 未结 5 1046
忘掉有多难
忘掉有多难 2020-12-29 05:01

Pointers in C++ may in general only be compared for equality. By contrast, less-than comparison is only allowed for two pointers that point to subobjects of the same complet

5条回答
  •  一整个雨季
    2020-12-29 05:39

    The problem is segmented architectures, where a memory address has two parts: a segment and an offset. It's "easy enough" to turn those pieces into some sort of linear form, but that takes extra code, and the decision was to not impose that overhead for operator<. For segmented architectures, operator< can simply compare the offsets. This issue was present for earlier versions of Windows.

    Note that "easy enough" is a systems programmer's perspective. Different segment selectors can refer to the same memory block, so producing a canonical ordering requires pawing through details of segment mapping, which is platform-dependent and may well be slow.

提交回复
热议问题