Boost logo

Boost Users :

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


At Tue, 14 Dec 2010 09:07:30 +0100,
Roman Perepelitsa wrote:
>
> [1 <multipart/alternative (7bit)>]
> [1.1 <text/plain; ISO-8859-1 (7bit)>]
> 2010/12/14 Dave Abrahams <dave_at_[hidden]>
>
> > 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));
>
>
> Using s2 causes undefined behavior (accessing an object through a
> pointer to unrelated type).

No, not if the types are layout-compatible. Look it up (it will cost
you a thorough reading ;->).

> reinterpret_cast has the same problem

reinterpret_cast causes implementation-defined, not undefined,
behavior IIRC, in the cases where the static_cast approach works.

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