Boost logo

Boost :

From: Moore, Paul (paul.moore_at_[hidden])
Date: 2001-01-22 10:39:49


Sorry to take up the list's time with this, but I don't know if this counts
as a bug in MSVC, or a problem with my understanding.

I'm in the process of changing class rational<I> to have explicit mixed mode
operators taking type I and type rational<I>. This turned out to be not too
hard, thanks to the operators.hpp header. However, I have a small problem
with operators - and /.

Not surprisingly, operators.hpp can't derive operator-(U, T) from
T::operator-=(U), as the operands are reversed. OK, it's another explicit
definition, but that's not an issue...

template <typename IntType> inline rational<IntType>
operator- (boost::call_traits<IntType>::param_type i, const
rational<IntType>& r)
{
    return rational<IntType>(i) -= r;
}

The problem is that, for a user-defined integer type MyInt,

    4 - rational<MyInt>(9,2)

doesn't compile. The error is

test.cpp(222) : error C2679: binary '-' : no operator defined which takes a
right-hand operand of type 'class boost::rational<class `anonymous
namespace'::MyInt>' (or there is no acceptable conversion)

Now, I can see how to derive this - define IntType as MyInt, promote 4 to
MyInt using the (available) conversion from int, and there's no problem. No
chain of 2 user-defined conversions, just a straightforward overload
resolution.

BUT, it does require the compiler to deduce that IntType should be MyInt.

Clearly, MSVC can't manage this - the question is, does the standard allow
this deduction - ie, is it MSVC or my understanding that is at fault? I
suspect it's MSVC, but I can't be sure.

On a related note, if it is MSVC that doesn't handle this, is there any
reasonable workaround? I can do something like

template <typename IntType, typename T> inline rational<IntType>
operator- (T i, const rational<IntType>& r)
{
    IntType ii = i; // Require that T will convert implicitly to IntType
    return rational<IntType>(ii) -= r;
}

I don't imagine that this can have any nasty side-effects, but I do worry...

Thanks,
Paul.


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