Home > Software engineering >  strict total order of std::less<Pointer>
strict total order of std::less<Pointer>

Time:09-28

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());
    }

CodePudding user response:

Yes, different arrays 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 multiple arrays since the partial order is exactly that the obvious relationship between indices and pointer order pertains within one array. (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”.)

  • Related