Boost logo

Boost Users :

From: Jean Llorca (yg-boost-users_at_[hidden])
Date: 2002-06-27 05:19:53


Hi,

I would like to propose, in the spirit of Bjarne Stroustrup book,
an upcast (where target inherits source).

template <class Target, class Source>
inline Target polymorphic_upcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
{

    typedef unsigned int uint;

    uint offset = reinterpret_cast<uint>( static_cast<Source*>(
reinterpret_cast<Target>(1) ) ) - 1;
    Target res = reinterpret_cast<Target>( reinterpret_cast<uint>( x ) -
offset );

    assert( res == dynamic_cast<Target>(x) );

    return res;
}

It would be faster to use the polymorphic_upcast, since it doesn't use
dynamic_cast in NDEBUG.

Regards,
Jean Llorca.


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