Boost logo

Boost :

From: Jens Maurer (Jens.Maurer_at_[hidden])
Date: 2001-01-12 14:15:42


"Moore, Paul" wrote:
> From: "Stephen Silver" <boost_at_[hidden]>
> > template <class T> rational(T n) : num(), den(1) { num = n; }
>
> Good catch - I'll add this.

Do make sure to document it right in the code. Any time I see such
code as "default initialize + assign" in foreign code, I assume
the code writer has not understood C++ at all.

> Hmm. Overall, I agree with this, although I wonder whether there could be
> cases where default-construct and assign is significantly slower than just
> constructing. Seems far less likely than even the explicit constructor case,
> though, and it's "only" performance, so I'd go with it.

What about a fixed-size bigint based on a C array which needs to be
default-initialized to 0 all over?

> Thinking some more, I suspect that we have to use this version as we must
> avoid constructing a rational<T> from a type U when the only constructor
> from U to T is explicit. So yes, I go with your code.

It's ugly. It's a template implicit conversion. :-(
For the problem at hand, it seems sufficient to have a rational
constructor which takes an "int" (or long) only. The template
approach is only really needed if there is some non-builtin
type U which can be converted to T, for example, another bigint
class. And in this case, we arguably invoke two user-defined
conversions (though technically speaking, we do it in two steps),
i.e. from U to T and from T to rational<T>. Not my favourite
perspective regarding complexity. Sorry, but listening to
Steve Adamczyk at the Toronto ISO C++ meeting explaining auto_ptr
implicit conversions built up a rather strong opinion against
complex conversion chaining.

I'd vote for the conservative "int only" approach.

Jens Maurer


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