Boost logo

Boost :

Subject: Re: [boost] numeric_cast
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2011-06-22 09:40:27


2011/6/22 Brandon Kohn <blkohn_at_[hidden]>:
> As written there is no way for a user to specify a different overflow
> handler, rounding policy, or range checker. The defaults are always used
> when numeric_cast is called.

May be i am missing something, but I see no problem here. numeric_cast
is designed to be as simple as possible. When the default polices are
not what you need, you just define your own converter function via

 template<typename Target, typename Source>
 inline Target my_numeric_cast ( Source arg )
 {
   using namespace boost::numeric;
   typedef boost::numeric::converter<Target, Source,
conversion_traits<Target, Source>, silent_overflow_handler > Converter
;
   return Converter::convert(arg);
 }

or just directly call the required conversion function:
converter<Target, Source, conversion_traits<Target, Source>,
silent_overflow_handler >::convert(arg);

So you do not need to create your own numeric_cast_traits. All you
need to do, as a developer of a new numeric type, is just overload
rounding functions for your type, add your type to
std::numeric_limits.

Using your solution, we will break thecurrent behavior of
numeric_cast function and the code that uses it. Library user won`t
know, will the numeric_cast truncate or round via ceil, will it throw
on overflows of silently ignore them. It will add more mess to the
library, and less users will use it.

Best regards,
Antony Polukhin


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