|
Boost : |
From: Dave Abrahams (abrahams_at_[hidden])
Date: 2000-05-30 15:05:59
--- In boost_at_[hidden], scleary_at_j... wrote:
> template <typename Base, typename Derived>
> Base * upcast(Derived * const d)
> { return static_cast<Derived *>(static_cast<Base *>(d)); }
>
> Should work for casting up hierarchy, fail for casting down or
across (with
> an error message like "cannot convert 'Derived *' to 'Base *'").
Will allow
> casting to the same type; a POSTULATE can easily be added to
prevent this if
> desired. More complicated tests not done :).
Nice. Who's maintaining casts.hpp? Can we put this in?
BTW, in my experience the hack someone suggested a while back to get
these things to work with MSVC6 doesn't work:
template <typename Base, typename Derived>
Base * upcast(Derived * const d, Base* = 0)
{ return static_cast<Derived *>(static_cast<Base *>(d)); }
The best thing I've been able to do, which isn't quite seamless, is
to build a class called upcast<Base> which is convertible to Base*
and has operator->() as well.
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk