Boost logo

Boost Users :

From: james.jones_at_[hidden]
Date: 2007-03-08 08:45:24


From: "Ovanes Markarian" <om_boost_at_[hidden]>
> As I wrote before, you can specialize numeric_limits class with your custom
> type:
>
>
> template<class T>
> struct my_numeric_limits_impl
> {
> static T min() { return boost::decimal::decimal_limits<T>::min(); }
> };
>
> namespace std
> {
> template<>
> class numeric_limits<your_custom_type1> : public
> my_numeric_limits_impl<your_custom_type1>
> {};
>
> template<>
> class numeric_limits<your_custom_type2> : public
> my_numeric_limits_impl<your_custom_type2>
> {};
>
> }
>
>
> What is wrong on this approach? The only problem I see here is the the
> customer must include the specialization of numeric limits. But this can be
> easily solved if you put in the same header where your_custom_type is
> defined. So everyone using yyour_custom_type will automatically include
> numeric_limits and if needed use the correct version.

I have no problem putting this in a standard include file. The problem I have is that I'm trying to make it as easy as possible for users to add their own models of my decimal type concept. I'd like for the steps to be as simple as: (a) define a new model and then (b) specialize is_decimal_type. So far I've been able to use (b) to drive lots of other functionality, and I'm frustrated that I can't get this to help with std::numeric_limits. But it sounds like there may be no perfect solution here.

Here's an example of how enable_if *has* helped me in this project. I'm defining a bunch of operators, e.g. operator+, which need to take arguments only of is_decimal_type objects. I can easily do this:

    template<typename LHSType,
             typename RHSType>
    typename enable_if_c<
                is_decimal_type<LHSType>::value ||
                is_decimal_type<RHSType>::value,
                typename result_type_2<LHSType, RHSType>::type>::type
    operator+(const LHSType& lhs, const RHSType& rhs)
    {
        typedef typename result_type_2<LHSType, RHSType>::type result_type;
        return result_type(operation_t::ADD, lhs, rhs);
    }

Works great, and doesn't interfere with other specializations/overloads of operator+.

-
James Jones Administrative Data Mgmt.
Webmaster 375 Raritan Center Pkwy, Suite A
Data Architect Edison, NJ 08837


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