|
Boost : |
From: Kevlin Henney (Kevlin.Henney_at_[hidden])
Date: 1999-09-02 11:59:21
Valentin Bonnard wrote:
Beman Dawes wrote:
> Kevlin Henney wrote:
> > template<class Derived, class Base>
> > inline Derived safe_downcast(Base* x)
> > {
> > assert(dynamic_cast<Derived>(x) != 0);
> > if(false)
> > dynamic_cast<Derived>(x);
> > return static_cast<Derived>(x);
> > }
> Kevlin, after wading throught the standard's description of
> dynamic_cast twice, I am still having trouble knowing which errors
> your added code will detect, and which will still get through. Could
> you enlighten me?
dynamic_cast from a base class w/ no virtual functions
to a derived class is ill-formed, but static_cast in
such case is well-formed.
That's the only difference in static semantic I can
think of for the moment.
It ensures both that the class is polymorphic and that it is a
base-derived conversion, otherwise the following would compile w/
NDEBUG:
const void *p = safe_downcast<const void *>("oops");
string s = safe_downcast<string>("oops again");
// etc
Kevlin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk