Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-10-05 16:07:43


Christoph Ludwig <cludwig_at_[hidden]> writes:

> On Tue, Oct 05, 2004 at 10:40:43AM -0400, David Abrahams wrote:

>> GCC claims that the operator+ provided by integer_arithmetic<number>
>> is ambiguous with the one that uses an implicit conversion to int
>> on each side of the operator. That's a compiler bug; the former is
>> an exact match.
>
> Are you sure? This particular error message starts
>
> In static member function `static PyObject*
> boost::python::detail::operator_l< op_add>::
> apply<L, R>::execute(L&, const R&) [with L = number, R = long int]'
>
> whence an exact match would be operator+(number const&, long int). And
> since number has both a non-explicit constructor taking a long and a
> operator long() const, I don't see why either of above operator+
> overloads should be a better match. Am I missing something?
>
> Regards
>
> Christoph
> --
> http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/cludwig.html
> LiDIA: http://www.informatik.tu-darmstadt.de/TI/LiDIA/Welcome.html
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

Jonathan Wakely <cow_at_[hidden]> writes:

>> GCC claims that the operator+ provided by integer_arithmetic<number>
>> is ambiguous with the one that uses an implicit conversion to int
>> on each side of the operator. That's a compiler bug; the former is
>> an exact match.
>
> Are you sure?
> (I don't doubt you are, just trying to understand it myself :)

I was sure, but I was wrong and you guys are both right.
The example needs to be changed:

  struct number
    : boost::integer_arithmetic<number>
  {
     explicit number(long x_) : x(x_) {}
     operator long() const { return x; }

      template <class T>
      number& operator+=(T const& rhs)
      { x += rhs; return *this; }

      template <class T>
      number& operator-=(T const& rhs)
      { x -= rhs; return *this; }

      template <class T>
      number& operator*=(T const& rhs)
      { x *= rhs; return *this; }

      template <class T>
      number& operator/=(T const& rhs)
      { x /= rhs; return *this; }

      template <class T>
      number& operator%=(T const& rhs)
      { x %= rhs; return *this; }

     long x;
  };

And, incidentally,

  class_<number>("number", init<long>())
                           ^^^^^^^^^^^^

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

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