From: Anthony Williams (anthony.williamsNOSPAM_at_[hidden])
Date: 2002-12-09 04:46:33


Eric Woodruff writes:
> Is my previous statement

> > Precisely, T1 is Foo in this case as you have already pointed out. This is
> > simply the identity conversion T1 -> T2 -> T1, which is pretty much the
> only
> > thing reinterpret_cast can do portably.

>only valid if placement new used a reinterpret_cast
> on it's implementation? Or if h.storage was reinterpret_cast-ed to a void*
> when it was passed in? Well, if reinterpret_cast remembered the type, it
> would know it wasn't what we were trying to cast it to.

The point is, you don't have a Foo* in the first place, you only have a char*,
unless you saved the result of placement new somewhere, in which case you
don't have a problem.

The argument that you can get to a Foo* given just a char/void * to the raw
storage using reinterpret_cast<> relies on the equivalence of
reinterpret_cast<void*> and static_cast<void*>, which I don't think is
justified.
 
> Also, instead of a placement_cast, why not write a placed_new<> template (as
> someone said, offset_cast is only used in this situation):
>
> template <typename Storage, typename Object>
> Object* const placed_new (Storage* const storage) {
> new (storage) Object;
> return ...;
> }
>
> // .. support construction with (1, 2, 3, ...) parameters as well

Because that gets you nothing vs just using placement new directly.
 
> "Eric Woodruff" <Eric.Woodruff_at_[hidden]> wrote in message
> news:aso09f$akc$1_at_main.gmane.org...
> >
> > "Anthony Williams" <anthony.williamsNOSPAM_at_[hidden]> wrote in
> > message news:15855.33204.759000.122558_at_gargle.gargle.HOWL...
> > > Eric Woodruff writes:
> > > > "Anthony Williams" <anthony.williamsNOSPAM_at_[hidden]> wrote in
> > > > message news:15855.9421.560000.173275_at_gargle.gargle.HOWL...
> > > > > Thus, given that h.storage is properly aligned, (which is the
> purpose
> > of
> > > > the
> > > > > other union member), after "new(h.storage) Foo", h.storage contains
> a
> > Foo

Anthony

-- 
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.