Boost logo

Boost Users :

Subject: [Boost-users] Please help with enable_if problem
From: John Dlugosz (JDlugosz_at_[hidden])
Date: 2010-04-13 19:18:22


I'm getting tons of this from Microsoft's compiler:

        error C2893: Failed to specialize function template 'enable_if<boo...

Reading the compiler docs, C2893 gives an example

        occurs because f's template parameter T is deduced to be
        std::map<int,int>, but std::map<int,int> has no member data_type
        (T::data_type can not be instantiated with T = std::map<int,int>.)

but isn't that the whole point of using enable_if? I'm using it already in my project and understand the idea behind it.

Here is the function in question:

        template <typename T, typename D>
        typename enable_if<
           boost::mpl::and_<
              std::tr1::is_base_of<generic_linear_time_ntbase, T>,
              boost::mpl::or_<
                 is_dimensional_time<D>,
                 std::tr1::is_same<duration,D>,
                 std::tr1::is_same<linear_duration,D>
>
>,
           T>::type&
        operator+= (T& left, const D& right)
         {
         left.addop_ (right); // real implementation is in the generic base class
         return *this; // but I need this template to return the correct derived (actual) class.
         }

Basically, I want to implement operator+= in a base class, but actual use is through derived classes of the common base. Rather than repeating a half dozen forms in each class that calls the common base and downcasts the return value, I made the real implementation in a named function, and this stand-alone function that calls it.

enable_if is used to make this += present only for left arguments that are derived from the class of interest, and a right argument that is two specific classes or otherwise noted with the predicate. I've successfully used the predicate (derives from true_type or false_type) in other uses of enable_if.

I've even mixed boost::mpl::or_ and and_ with the compiler-supplied TR1 before, with something quite similar. Here's one that does work:

        template <typename T, typename D>
        typename enable_if<
           boost::mpl::and_<is_Universal_time_API<T>, boost::mpl::or_<is_Universal_duration_API<D>,is_dimensional_time<D> > >,
           T>::type operator+ (const T& t1, const D& d1)
         {
         T temp (t1);
         temp += d1;
         return temp;
         }

My predicates are based on the native TR1's true_type/false_type.

Thanks,
--John






TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.


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