Boost logo

Boost Users :

Subject: Re: [Boost-users] Flyweight: wrapping shared_ptr
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2014-10-08 14:57:44


Akim Demaille <akim <at> lrde.epita.fr> writes:

>
> I used something more agressive:
>
> [...[
>
> /// Conversion to superclass.
> template <typename U>
> operator const poly_flyweight<U>&() const
> {
> const void* me = this;
> return *static_cast<const poly_flyweight<U>*>(me);
> }

Wow, this is totally verboten. The following problems lurk:

1. poly_flyweight<U> need not be layout compatible with
poly_flyweight<T>.
2. Even if #1 holds, a pointer to Base need not be numerically
equivalent to a pointer to Derived (in the presence of multiple
inheritance, for instance.)
3. Even if #2 holds, the thing would work by pure chance (basically,
because a "casted" poly_flyweight<Base> happens never to touch
the internal factory of poly_flyweight<Derived>, which in principle
a conformant implementation is allowed to do).

All in all, the thing could potentially work, but why would that be
preferred over derived_poly_flyweight<Derived,Base>?

Joaquin M López Muñoz
Telefónica


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