Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2002-11-20 15:12:09


From: "Eric Woodruff" <Eric.Woodruff_at_[hidden]>
>
> Every reference I can find excludes pointer conversions from being
> implementation defined. They all (even the standard) specifically treat
> pointer conversions differently (clause 7) than pointer->integer->pointer
> conversions. This is because creating an integer requires mapping the bits
> to a temporary, converting pointers does not require this because pointers
> share the same underlying representation, and it is done in compile time so
> there is no possibility of translation occuring.
>
> "reinterpret_cast treats all pointers exactly as traditional type-casting
> operators do. "

Let me outline what I see in the standard. Perhaps I can clarify
the matter for you and others, late to the debate though I may be.

5.2.10/1 says only the conversions listed in the following
paragraphs are permissible with reinterpret_cast. IOW, if it
isn't explicity stated, it isn't allowed.

Paragraph 2 states that you cannot cast away constness.

Paragraph 3 states unequivocally that the mappings are
implementation-defined.

Paragraph 7 states that you can use reinterpret_cast to cast from
one pointer type to another. It also states that you can convert
from one pointer type to another and back again, yielding the
original pointer value. Finally, it states that all other
pointer conversions, which includes the conversion from one type
to another (but not back again) is unspecified. IOW, the only
thing you can do with reinterpret_cast on pointers that is
portable is cast from one pointer type to another and back again.

For reference, 1.3.5 states that "implementation-defined" means
that what you get is entirely up to the implementation and that
the only obligation on the implementor is to document what
happens.

Likewise, 1.3.13 states that "unspecified behavior" means that
what you get is entirely up to the implementation and the
implementor doesn't even need to document what happens.

So, 5.2.10/7 plainly states that there's only one thing you can
do portably with reintrepret_cast on pointers: convert from one
type to another and back again. Everything else is unspecified,
so you can't even count on finding the result documented on each
platform of interest. This means that your references and
understanding are wrong if they lead you to conclude that one-way
pointer conversions are anything other than unspecified.

now-removing-my-language-lawyer-interpreter's-hat-ly y'rs,

Rob

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk