How can pointers be totally ordered?

前端 未结 5 1070
忘掉有多难
忘掉有多难 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:21

    Is it possible to implement the standard library on targets where pointers do not form a global, total order?

    Yes. Given any finite set you can always define an arbitrary total order over it.

    Consider a simple example where you have only five possible distinct pointer values. Let's call these O (for nullptr), γ, ζ, χ, ψ1.

    Let's say that no pair of two distinct pointers from the four non-null pointers can be compared with <. We can simply arbitrarily say that std::less gives us this order: O ζ γ ψ χ, even if < doesn't.

    Of course, implementing this arbitrary ordering in an efficient manner is a matter of quality of implementation.


    1 I am using Greek letters to remove subconscious notion of order that would arise due to familiarity with the latin alphabet; my apologies to readers that know the Greek alphabet order

提交回复
热议问题