Boost logo

Boost :

Subject: Re: [boost] [numeric_cast] request for boost::numeric_downcast
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2008-11-11 06:46:17


David Abrahams skrev:
> on Thu Nov 06 2008, Fernando Cacciola <fernando.cacciola-AT-gmail.com> wrote:
>
>> Hi Thorsten,
>>> Hi Fernando,
>>>
>>> Amoung our utilities we have
>>>
>>> boost::polymorphic_cast
>>> boost::polymorphic_downcast
>>>
>>> The latter only performs a check in debug builds, and can then be used whenever the
>>> cast must succeed.
>>>
>> Oh... I wasn't aware of _downcast.
>>
>>> boost::numeric_cast has no such alternative. Therefore I wuold like to see one. To
>>> keep in the naming convention from above, I think it could be reasonable to call
>>> this
>>>
>>> boost::numeric_downcast
>>>
>>> Furthermore, this should if possible be implemented in a seperate header (which is
>>> included in the main header) that can be compiled without exception-support. I think
>>> there are still platforms where they need this functionality, but don't want to sue
>>> exceptions.
>>>
>> Indeed.. it all makes sense.
>
> Not to me. In what sense is this proposed cast "down?"

Well, as you say, its narrowing.

The naming applied to polymorphic_cast and polymorphic_downcast is not
perfect either, since both can be used to "casting down".

> The point of boost::polymorphic_downcast is that it can be used for
> casting down the inheritance hierarchy, to more-derived classes. It
> cannot, however, be used for cross-casts as polymorphic_cast can.
>
> boost::polymorphic_downcast is meant to be used where you think you
> really know what derived type you've got, and you want an assertion to
> reinforce your reasoning with a runtime check. I suppose you can have a
> similar situation for numeric types (e.g. "I know this long int should
> fit in a short int").
>
> How about boost::checked_cast, or boost::checked_narrowing_cast or
> boost::narrow_cast?

I proposed numeric_downcast because I wanted "downcast" to mean an
debug-checked version of _cast. There are quite a few casts in various
boost libraries (any, smart pointer), and I think it would be nice if we
could come up with a consistent naming convention this type of cast.

How about

        boost::numeric_debug_cast
         boost::polymorphic_debug_cast
         boost::any_debug_cast

?

-Thorsten


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