Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 1999-09-21 14:51:36


Darin Adler wrote:

I realized what the problem is.

> template <class Derived, class Base>
> inline Derived new_polymorphic_downcast(CastFrom* from)
> {
> Derived result(static_cast<Derived>(from));
> assert(dynamic_cast<Derived>(from) == result);
> return result;
> }
> // The following fails,

Yes ! It has undefined behaviour. This is a FAILURE.

> new_polymorphic_downcast<D2*>(pb);
> }

I suggest the following implementation for new_polymorphic_downcast:

    template <class CastTo, class CastFrom>
    inline CastTo new_polymorphic_downcast(CastFrom* from)
    {
        assert(dynamic_cast<CastTo>(from) == from);
        return static_cast<CastTo>(from);
    }

> >> - want cast that works like static_cast<>, but only for casting from
> >> void* since that's a common reason to use it
> >
> > Well, void* to X* conversion is just another
> > THIS-IS-REALLY-A<X>, which is generally done
> > by static_cast.
>
> Most casts are THIS-IS-REALLY-A<X>.

> legal uses of const_cast are too.

Not really. It depends.

-- 
Valentin Bonnard

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