Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2004-01-26 14:33:06


"Andy Little" <andy_at_[hidden]> escribió en el mensaje
news:bv1kl9$me6$1_at_sea.gmane.org...
> Thanks Fernando Cacciola...The converter is great ... I love it... :-)
>

:-) You're welcomne.

> #include <iostream>
> #include <limits>
> #include "boost/numeric/converter.hpp"
>
> template<typename T, typename S>
> struct converter : boost::numeric::converter<
> T,
> S,
> typename boost::numeric::conversion_traits<T,S>,
> typename boost::numeric::def_overflow_handler,
> boost::numeric::Round<
> typename boost::numeric::conversion_traits<
> T,
> S
> >::source_type
> >,
> boost::numeric::raw_converter<
> typename boost::numeric::conversion_traits<
> T,
> S
> >
> >,
> boost::numeric::UseInternalRangeChecker
> >{};
>
> //Of course could specialise converter<X,Y>
>
> // value_type_traits
> // Value_type with user definable conversion
> template<
> typename Value_type,
> template<
> typename T,
> typename S
> > class Converter
> >
> struct value_type_traits
> {
> typedef Value_type value_type;
> template <typename V>
> static Value_type convert_to_value_type( V v)
> {
> return Converter<Value_type,V>::convert(v);
> }
> template <typename V>
> static V convert_from_value_type( V v)
> {
> return Converter<V,Value_type>::convert(v);
> }
> };
>
> int main()
> {
> try{
> typedef value_type_traits<int,converter> v_traits;
> //whatever
> std::cout << v_traits::convert_to_value_type(100000000.5) <<'\n';
> std::cout << v_traits::convert_from_value_type(65535) << '\n';
> }
> catch (std::exception e){
> std::cout << "failed with " << e.what() << '\n';;
> }
> }
> /*output:
> 100000001
> 65535
> */
>
I see.
The idea is that the user of value_type_traits plugs in her own converter,
which can be easily composed out of boost::numeric::converter by derivation.
Looks nice to me.

Cheers,

Fernando Cacciola
SciSoft


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