Boost logo

Boost Users :

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2005-05-30 05:45:02


Fernando Cacciola wrote:

<snip>

> int const& convert( int const& s ) { return s ; }
>
> Notice that it takes a reference and returns a reference.
>
> As such,
>
> numeric::converter<int,int>::result_type is "int const&"; that is, a
> reference.
>
> and:
>
> numeric::converter<int,int>::argument_type is also "int const&"
>

<snip>

>
> A "fix" on your side would be to use
> ::boost::numeric::converter<Target,Source>::argument_type;
> but this isn't practical because users are forced to specify "Source"
> explicitely.

but in your example above you say that both 'result_type' and
'argument_type' result in a 'int const&' ? So replacing it would'nt make
  sens? I suppose 'argument_type' results in 'int', right ?

>
> So you need "Source" to be the source type, but then, you also _need_
> "Target" to be the result type:
>
> template<typename Target, typename Source>
> inline Target convert2 ( Source arg )
> {
> return ::boost::numeric::converter< Target, Source >::convert( arg ) ;
> }
>
> Incidentally, boost::numeric_cast<> (the new one) is equivalent to your code
> so is just as equally buggy.
>
> I'm afraid I have to patch it not to use result_type anymore.
>
> P.S: One could thing that the traits class, in the case of a trivial
> conversion, could define bare types instead of references (that is, making
> result_type being just "T" instead of "T const&). Altough this OK for
> builtin types, it isn't for the UDT, and it is precisly in the case of UDTs
> were the "reduction to a no-op in the trivial case" becomes really
> important. This means though that numeric_cast<> is better not used with
> UDTs in generic context when T==S is possible (because the optimization is
> then lost)
>
> P.S.2: Of course the alternative is to use "Source const&" instead of
> "Source"... I don't really remember the details but I seem to recall that
> the original numeric_cast<> used a non-reference argument for some good
> reason; and so I just kept that.
>
>
> Thanks for the report!
>
> Fernando Cacciola
> SciSoft
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
Check out our training program on acoustics
and register on-line at http://www.fft.be/?id=35

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net