Boost logo

Boost Users :

Subject: Re: [Boost-users] metaprogramming help
From: Dave Abrahams (dave_at_[hidden])
Date: 2010-12-13 19:29:13


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));

HTH,

-- 
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