Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strict total order of std::less<Pointer>

This question comes from this comment:

If you have say two vectors a and b, the total order is permitted to be &a[0], &b[0], &a[1], &b[1], &a[2], &b[2], ..., i.e., with the elements interleaved.

Is that order permitted?

I don't known much about the standard. That seems correct if I read only sections directly related to std::less.

And I found that Herb Sutter's gcpp library have a similar usage(link):

    //  Return whether p points into this page's storage and is allocated.
    //
    inline
    bool gpage::contains(gsl::not_null<const byte*> p) const noexcept {
        //  Use std::less<> to compare (possibly unrelated) pointers portably
        auto const cmp = std::less<>{};
        auto const ext = extent();
        return !cmp(p, ext.data()) && cmp(p, ext.data() + ext.size());
    }
like image 356
VainMan Avatar asked Sep 24 '21 06:09

VainMan


Video Answer


1 Answers

Yes, different arrays (that are not part of the same complete object) can be interleaved in the ordering, but each array must separately be ordered correctly—this is what it means that the total order must be consistent with the partial order established by the built-in operators. The fact that a+1 points to the element immediately after *a is irrelevant to the question of unrelated arrays since the partial order is exactly that the obvious relationship between indices and pointer order pertains within one complete object. (In fact, “immediately after” is circular here, since the only observable immediacy is in the array indices themselves. Integer casts need not respect it, so you can’t see “the real addresses”.)

like image 65
Davis Herring Avatar answered Nov 04 '22 13:11

Davis Herring