|
Boost Users : |
Subject: Re: [Boost-users] metaprogramming help
From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2010-12-14 04:44:12
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.
Sebastian
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