Boost logo

Boost :

From: Stephen Silver (boost_at_[hidden])
Date: 2001-01-12 19:35:22

Paul Moore wrote:

> > template <class T> rational(T n) : num(), den(1) { num = n; }
> One issue. This clashes with the existing constructor
> rational(IntType n = 0) : num(n), den(1) {}
> In principle, I can omit this and just use your version. However,
> there are some issues. The first is minor - I would prefer to
> explicitly code a constructor from the base integer type, for clarity.

Then why not do so? There's no problem with having both - that's
how I've been doing it. The non-template one takes preference in
overload resolution.

> The second is that I now have to write a constructor for 0
> arguments explicitly.

I actually prefer this. For clarity. :-)

> The third, and most major, is that the
> template class uses assignment, but for construction from IntType
> it would be better to use construction (no issue with an explicit
> constructor, and construction may be more efficient...)

George Heintzelman posted a neat solution to this. Using
implicit_cast, the template constructor should be just as
efficient as the non-template one, without any risk of using
an explicit conversion. (Except that the paramater type needs
to be always const T&, because call_traits<T>::param_type doesn't
work in this context.)

> [Later...]
> I implemented and tested this, and it's giving odd errors in the tests - of
> which
> rational_example.cpp(50) : error C2678: binary '+' : no operator defined
> which takes a left-hand operand of type 'class boost::rational<int>' (or
> there is no acceptable conversion)
> is an example.

I don't get any errors (with GCC 2.95.2 or Borland C++ 5.5.1), even
if I remove the non-template constructor as you have done. Is this
another MSVC++ template bug?


Boost list run by bdawes at, gregod at, cpdaniel at, john at