Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-06-03 06:58:47


Terry Golubiewski wrote:
> Rob Stewart wrote:

Please, people, don't drop attributions and keep them simple.

> >> const endian<big, T>* src = reinterpret_cast<const
> endian<big, T>*>(buf);
> >> endian<native, T>* dst = reinterpret_cast<endian<native, T>*>(buf);
> >>
> >> (void) copy(src, src+numTs, dst);
> >
> > A call to swap_in_place() avoids the ugly reinterpret_casts
> > and looks far simpler.
>
> swap_in_place still needs a reinterpret_cast too to convert
> from the 'char * buf' assembled from the hypothetical TCP stream.

Not quite. As discussed in a separate post, a static_cast is sufficient and there's only one cast needed. That's still less ugly. (Since buf is a void * in the snipped code, static_cast would work for your casts, too.)

> What bugs me is that 'buf' doesn't point to a 'T' yet. It
> points to a half-baked 'T'. Only after one calls
> swap_in_place() is the object really a 'T'.

That would apply to floating point, certainly. However, for integer types, your statement is false. Before the swap, they are still integers, they just don't have the desired value.

Even in the object-based approach, the object isn't a proper T until you access it and the swap occurs. The implicit swap veneer just gives you the impression that it is already in the desired form.

> The reinterpret_casts are valuable here because they show that the
> programmer is doing a potentially dangerous thing -- changing
> the memory underlying a structure in place!

Sure, but why make things uglier than necessary?

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
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