Boost logo

Boost :

Subject: Re: [boost] [Endian] Performance
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-09-13 15:29:18


John Filo wrote:
>
> Apologies for getting a bit off track re: this review, but the
> one thing I don't understand from 5.2.10 of the standard:
>
> -7- A pointer to an object can be explicitly converted to a
> pointer to an object of different type. Except that
> converting an rvalue of type ``pointer to T1'' to the type
> ``pointer to T2'' (where T1 and T2 are object types and where
> the alignment requirements of T2 are no stricter than those
> of T1) and back to its original type yields the original
> pointer value, the result of such a pointer conversion is
> unspecified.
>
> The last sentence seems to contradict itself. It first says
> that the following assertion will never trigger as long as
> T2's alignment requirements are not stricter than T1's.
>
> void f(T1 *arg)
> {
> T2 *p2 = reinterpret_cast<T2*>(arg)
> T1 *p1 = reinterpret_cast<T1*>(p2);
> assert(p1 == arg);
> }

Right.

> But then it goes on to say that the "conversion is
> unspecified".

No. It says, "Except that converting ... 'pointer to T1' to ... 'pointer to T2' ... and back ... yields the original pointer value, the result ... is unspecified."

What you did in f() is fine. Using p2 in any other way is unspecified behavior. That means what you get is up to the compiler vendor/version. It may be documented, but probably isn't. Thus, it is in no way portable.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com

________________________________

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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