Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2000-12-08 03:19:00


Jesse Jones wrote:
> Does it really show the intent? I don't know about you, but I have real
> problems remembering the difference between polymorphic_cast and
> polymorphic_downcast. I suspect most users won't have a clue what either
> one means. It's true that they can look up the definition, but this seems
> like an unneccesary burden to place on readers.

Well, I haven't found a use case for 'polymorphic_cast' yet (in my code in all
situations when a cross-cast is needed (or possible) a failure of the cast is
also ok or even expected), so I have no problems with differentiation of these
two ;). More seriously, I think that 'downcast' is a quite expressive word,
showing you exactly what happens behind the scene.

> >/**/ }". Also, speaking about expressiveness, consider passing the
> result of
> >the cast to a function:
> >
> >Derived* derived = dynamic_cast<Derived*>(base);
> >assert(derived != NULL);
> >foo(derived);
> >
> >vs.
> >
> >foo(polymorphic_downcast <Derived*>(base));
> >
> >I certainly prefer the last form.
>
> I would put the assert in foo. :-)

I would too, if I were the author of 'foo' :). But, (1) it can be not the
case, and (2) a function may take a reference to 'Derived', not a pointer, so
you'll have to perform the check on the caller's side.

--Aleksey


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