3 ms·
This remind me of a story. When Stephanov was implementing the STL, he needed a strict weak ordering among all pointers to implement things like std::map. Ins
by gpderetta 10y ago
This remind me of a story.
When Stephanov was implementing the STL, he needed a strict weak ordering among all pointers to implement things like std::map.
Instead of wasting time arguing within the committee on wether operator< should have the required semantics, he simply added std::less as a primitive and default comparison for std::map. It is specified to call operator< for every T, except that for pointer it relies on unspecified compiler support for doing the right thing.
On pretty much all implementations it simply calls operator< even for pointers.
- tehrei 10y agoOh, so then on C++ you can check if pointer-to-T p is within T a[sz]; by doing (a == p || std::less(a, p)) && (std::less(p, a + sz))
- gpderetta 10y agoPossibly not. The standard guarantees that less yields a total order for pointers. But the ordering itself is unspecified, and not even guaranteed to be consistent with operator< for pointers to elements of the same array.
- yoklov 10y agoI thought it was UB to use < on pointers outside of the same block of memory. e.g. your function is fine if the answer is true, but UB if the answer is false. I could be misremembering though, but I do know I'd always cast to uintptr_t for this.