Boost logo

Boost Users :

From: jono (jp_at_[hidden])
Date: 2007-11-13 21:54:28


hello all.

i have a boost::static_visitor that delegates to a static function to
enable partial specialization...

/// the templates & so on are declared as so ......
///
///
/// =============================================================

typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, LESS} BinaryOp;

template<BinaryOp op, typename ObjectT>
    Object binary_op(const ObjectT &arg1, const ObjectT &arg2) {return
undefined;}

template<BinaryOp op, typename ObjectT, typename ObjectU>
    Object binary_op(const ObjectT &arg1, const ObjectU &arg2) {return
undefined;}

template<BinaryOp op>
class BinaryOpVisitor : public boost::static_visitor<Object>
{
 public:

    template<typename ObjectT, typename ObjectU>
        Object operator()( const ObjectT &arg0, const ObjectU &arg1) const
        {
            return binary_op<op, ObjectT, ObjectU>(arg0, arg1);
        }

    template<typename ObjectT>
        Object operator ()(const ObjectT &arg0, const ObjectT &arg1) const
        {
            return binary_op<op, ObjectT>(arg0, arg1);
        }
};

/// =============================================================
///
/// then i have specialized the binary_op function as follows....
///
/// =============================================================

template<> Object binary_op<ADD>(const Integer& arg0, const Integer& arg1)
    {return make< Integer >(arg0 + arg1);}

template<> Object binary_op<ADD>(const Real& arg0, const Real& arg1)
    {return make< Real >(arg0 + arg1);}

/// =============================================================
///
/// the problem arises when i want to apply an operator to
heterogenous but convertible types, eg: Integer + Real
///
/// =============================================================

template<> Object binary_op<ADD>(const Integer& arg0, const Real& arg1)
    {return make< Real >(arg0 + arg1);}
            
template<> Object binary_op<ADD>(const Real& arg0, const Integer& arg1)
    {return make< Real >(arg0 + arg1);}
           

/// =============================================================
///
/// as shown, i can get the result i want by explicitly specializing
each combination of operands,
/// but if i add a Complex, then a Rational, then a Modulus and so
on, i end up with a combinatorial explosion.
///
/// i would like to find a way of doing all this with a single
specialization for each pair of heterogenous operands,
/// such that they can be accepted in any order...
///

any takers?

cheers

Jono Poff

Day One Digital Media
Auckland, NZ


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