Boost logo

Boost Users :

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


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.

Jean Llorca.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at