Boost logo

Boost :

Subject: Re: [boost] numeric_cast
From: Brandon Kohn (blkohn_at_[hidden])
Date: 2011-06-22 08:14:14


On 6/21/2011 4:33 PM, Antony Polukhin wrote:
> First of all, it looks like the numeric_cast library is not maintained
> any more, so you won't get a fast answer.
>

I was hoping the community might step up on this.

>
> There is a chapter called "Type Requirements and User-defined-types
> support" in numeric_cast library, that describes how to make your new
> numeric type usable with numeric_cast.

I've read it.

>
> Chapter "converter<> function object" describes how to use
> boost::numeric::converter with different casting polices.
>
> Did I answered your questions or have i missed something?
>

I think you've missed something.

Here is the code for numeric_cast that is in boost:

   template<typename Target, typename Source>
   inline
   Target numeric_cast ( Source arg )
   {
     typedef boost::numeric::converter<Target,Source> Converter ;
     return Converter::convert(arg);
   }

Note the way Converter is defined. Here is the definition of converter:

template<class T,
          class S,
          class Traits = conversion_traits<T,S>,
          class OverflowHandler = def_overflow_handler,
          class Float2IntRounder = Trunc< BOOST_DEDUCED_TYPENAME
Traits::source_type> ,
          class RawConverter = raw_converter<Traits>,
          class UserRangeChecker = UseInternalRangeChecker
>
struct converter : convdetail::get_converter_impl<Traits,
                                                   OverflowHandler,
                                                   Float2IntRounder,
                                                   RawConverter,
                                                   UserRangeChecker
>::type
{

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.

Regards,

Brandon Kohn


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