Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-11-02 10:39:32


----- Original Message -----
From: <helmut.zeisel_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, November 02, 2001 11:43 AM
Subject: [boost] Re: numeric_cast, VC++ 6.0, specialization.

> --- In boost_at_y..., "Fernando Cacciola" <fcacciola_at_g...> wrote:
> > Does this compile?
> >
> > int main()
> > {
> > double d=5;
> > typedef ::boost::numeric_cast_traits<int,double> traits ;
> > int j = traits::converter::cvt(d);
> > }
> >
>
> Yes.
>

Well, I got back to the linux box and find out the following:

My version of numeric_cast<> looks like this:

  template<typename Target, typename Source>
  inline
  boost::numeric_cast_traits<Target,Source>::ret_type
  numeric_cast ( boost::numeric_cast_traits<Target,Source>::arg_type arg
                 BOOST_EXPLICIT_DEFAULT_TARGET
                )
  {
    return boost::numeric_cast_traits<Target,Source>::converter::cvt(arg);
  }

And you call it with this: numeric_cast<int>(d); which, BTW, is the way it
is supposed to be called.

But there is a problem here... since you are not actually supplying the
Source type explicitely, it must
be deduced from the argument type.
The argument type is double, but the parameter type is
"boost::numeric_cast_traits<Target,Source>::arg_type"
so the compiler has now way to infer the actal type of Source.

If you had called it with: numeric_cast<int,double>(d) ; it would have
worked, but this is not the intended usage.

OTOH, "boost::numeric_cast_traits<Target,Source>::arg_type" is intended to
support user
defined types allowing the cast to take the parameter by reference instead
of value.

It seems that we have to forget about the argument type optimization in the
meantime...

Notice that the return type optimization does work (because the Target type
is explicit),
and this is the really important optimization, because it allows the case of
numeric_cast<T,T> -the trivial conversion- to by entirely optimized away.

So, you can change this:

  template<typename Target, typename Source>
  inline
  boost::numeric_cast_traits<Target,Source>::ret_type
  numeric_cast ( boost::numeric_cast_traits<Target,Source>::arg_type arg
<============
                 BOOST_EXPLICIT_DEFAULT_TARGET
                )
  {
    return boost::numeric_cast_traits<Target,Source>::converter::cvt(arg);
  }

to this:

  template<typename Target, typename Source>
  inline
  boost::numeric_cast_traits<Target,Source>::ret_type
  numeric_cast ( Source arg <============
                 BOOST_EXPLICIT_DEFAULT_TARGET
                )
  {
    return boost::numeric_cast_traits<Target,Source>::converter::cvt(arg);
  }

and it will work (I think...)

I'll upload the change as soon as I can.

Thanks for the help!

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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