Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2004-01-25 18:43:21


Thanks Fernando Cacciola...The converter is great ... I love it... :-)

#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
*/


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