Boost logo

Boost Users :

Subject: Re: [Boost-users] metaprogramming help
From: Dave Abrahams (dave_at_[hidden])
Date: 2010-12-14 05:17:15


At Tue, 14 Dec 2010 10:44:12 +0100,
Sebastian Redl wrote:
>
> On 14.12.2010 01:29, Dave Abrahams wrote:
> > At Mon, 13 Dec 2010 18:52:34 -0500,
> > John Dlugosz wrote:
> >> [1<multipart/alternative (7bit)>]
> >> [1.1<text/plain; us-ascii (quoted-printable)>]
> >> A couple of times I've run into the situation where a "new" C++ class is written to exactly match "legacy" C structures. At the edges where old code meets new code, you can cast one to the other in-place. For example, given a huge mess of data that includes
> >> oldstruct s1;
> >> you could write the line
> >> newclass& s2= reinterpret_cast<newclass&>(s1);
> >> because s2 is designed to exactly overlay s1.
> > Unless oldstruct and newclass are both POD, the above has
> > implementation-defined behavior. The portable way to do it, if they
> > indeed have the same layout, is
> >
> > newclass& s2= *static_cast<newclass*>(static_cast<void*>(&s1));
> 5.2.10p7 in the current draft defines the semantics of
> reinterpret_cast between standard-layout types via this double
> static_cast, so there is no difference.

That's an draft for C++0x. I assume the OP wants the code to work
under C++03.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net