C++ Logo

SG12

Advanced search

Subject: Re: [ub] Justification for < not being a total order on pointers?
From: Gabriel Dos Reis (gdr_at_[hidden])
Date: 2013-10-17 18:06:08


James Dennett <jdennett_at_[hidden]> writes:

| On Thu, Oct 17, 2013 at 12:41 PM, Gabriel Dos Reis <gdr_at_[hidden]> wrote:
| > James Dennett <jdennett_at_[hidden]> writes:
| >
| > | On Thu, Oct 17, 2013 at 8:19 AM, Gabriel Dos Reis
| > | <gdr_at_[hidden]> wrote:
| > | > Nevin Liber <nevin_at_[hidden]> writes:
| > | >
| > | > | But, even if segmented architectures, unlikely though it is,
| > | > | do come back, the
| > | > | ordering problem still has to be addressed, as std::less<T*>
| > | > | is required to
| > | > | totally order pointers. I just want operator< to be an
| > | > | alternate spelling for
| > | > | that property.
| > | >
| > | > I will repeat this again: std::less<T*> is absolutely not a problem,
| > | > because this
| > | >
| > | > return intptr_t(p) < intptr_t(q);
| > | >
| > | > is valid and portable definition that requires no other special handling.
| > |
| > | I think it's not useful though; nothing guarantees that p == q =>
| > | intptr_t(p) ==intptr_t(q), so far as I know,
| >
| > How do you arrive to this conclusion?
|
| As always, it's hard to point to where something is _not_ specified.
|
| I'm aware of no such guarantee. If you're aware of one, you can
| presumably cite it, and I'd appreciate such a citation.

The question wasn't rethorical -- it was genuine. In fact, I appreciate
a lot that you're giving it a mature and responsible thought and answer.

However, the reason why I asked is that your statement is essentially this:

    template<typename T>
    T id(T t) { return t; }

    int* p = acquire_some_valid_pointer();
    auto a = intptr_t(p); // get the integer mapping of p;
    auto b = intptr_t(id(p)); // to the "same" think again.
    assert (a == b); // #1

If I read your statement correctly, you are saying that the assertion on
line #1 isn't guarantee to succeed. If that is true, then I believe it
is plainly a defect in the language and a CWG business.

| All I see is
| "A pointer can be explicitly converted to any integral type large
| enough to hold it. The mapping function is implementation-defined."
| We can debate whether the word "function" there should be taken in a
| pure mathematical sense, and whether "a pointer" means the value
| rather than the representation, and if we have just the right reading
| (it's a function on pointer values) then we get that equal pointers
| have equal values when converted to intptr_t. We don't have any
| guarantee about whether it's an order-preserving function though,
| unless that's somewhere else.

-- Gaby


SG12 list run by herb.sutter at gmail.com