Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2005-09-06 05:09:22


Hello,

I have a problem. I would like to use standart C++ <functional> function wrappers, but have a
problem. At least in MS VC 7.0 a binary_function derivate (like plus) is defined:

template<class Type>
   struct plus : public binary_function <Type, Type, Type>
   {
      Type operator()(
         const Type& _Left,
         const Type& _Right
      ) const;
   };

So if I am goint to add int and double value I will get int back. How can find a work around?
First I was thinking about smth like:

template<class _Arg1, class _Arg2>
   struct _ResultType
{
//some general implementation like
   typedef typename _Arg1 result_type;
};

//specializations like:
template<>
   struct _ResultType<long, double>
{
   typedef double result_type;
};

template<class _BinaryFct, class _ResultType>
   struct Expression
{
   typedef typename _ResultType::result_type result_t;
   typedef typename _BinaryFct<result_t> binary_fct;

   //other definitions etc...
};

somewhere in the code:
#include <functional>

Expression<std::plus, _ResultType<long, double> > myExpr;

A C++ compiler awaits a real type which causes an error. How else can I change the type of
plus<long> to plus<double>.

Probably I could write smth like this:

typedef std::plus<_ResultType<long, double> > myBinaryFunction;
Expression<myBinaryFunction> myExpr;

But somehow I don't like it, since the _ResultType class will contain more information as simple
result_type. And I will need to pass it twice like:

typedef std::plus<_ResultType<long, double> > myBinaryFunction;
Expression<myBinaryFunction, _ResultType<long, double> > myExpr;

This make the code not very readable...

Is there any way using boost to overcome this?

With Kind Regards,

Ovanes Markarian


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