Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2004-07-16 15:05:06


"Neal D. Becker" <ndbecker2_at_[hidden]> wrote

> I found numeric::ublas::promote_traits to be pretty useful. This tiny
> addition is also useful to go the other direction:
>
> #include <complex>
>
> template<typename T>
> struct Scalar {
> typedef T type;
> };
>
> template<typename T>
> struct Scalar<std::complex<T> > {
> typedef T type;
> };

I found this scheme useful to do this job. BTW, works as is on complex
because complex has a value_type member:
Only needs to be specialised if UDT has no value_type member typedef:

    template <typename T>
    struct to_arithmetic; /// whatever name...

    namespace detail{

        template<bool Condition,typename T>
        struct apply_value_type_if;

        // if T is not arithmetic
        // recursively apply to get to arithmetic type
        template<typename T>
        struct apply_value_type_if<true,T>{
            typedef typename to_arithmetic<
                typename T::value_type
>::type type;
        };

        //if T is arithmetic
        template<typename T>
        struct apply_value_type_if<false,T>{
            typedef T type;
        };
    }

    template <typename T>
    struct to_arithmetic : detail::apply_value_type_if<
        (boost::is_arithmetic<T>::value == false),
        T
>{};

regards
Andy Little


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