Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-12-06 12:48:22


Is my previous statement 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.

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

"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
> > > > object. Thus accessing it through a pointer-to-Foo is legal, as Foo
> is the
> > > > dynamic type of the object.
> > > >
> > >
> > > This is precisely my reasoning why reinterpret_cast<> is _not_
> > > implementation defined.
> > >
> > > It must be the case that it is equal to the situation of having
> > >
> > > Foo* -> char */void* -> Foo* if Foo is the dynamic type of the
object.
> >
> > Nowhere do I see a requirement that supports your deductions. Indeed
there
> are
> > very few requirements on reinterpret_cast<>, other than the fact that
what
> it
> > does must be defined by the implementation. For pointers to objects,
these
> > amount to:
> >
> > * T1* can be converted to T2* and back again unchanged (unless T2 has
> stricter
> > alignment requirements).
>
> 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.
>

[snip]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk