|
Boost : |
From: Kevlin Henney (Kevlin.Henney_at_[hidden])
Date: 1999-12-06 08:53:37
> template<typename Int, bool normalize = true>
> class rational
> {
> ...
> };
The difficulty with this is two-fold. First (a minor one),
rational<int,false> doesn't seem to me to be an intuitive way of getting
denormatised fractions of ints.
Yup, I admit the naming's not so hot :->
A better proposal would be:
enum normalization { normalized, denormalized };
template<typename Int, normalization Normalization = normalized>
class rational;
The more important issue is that we simply
have two largely disjoint implementations (the guts of the implementation
is
so small that it would be hard for *any* implementation variation not to
affect everything!), and triggering the distinction via a template
parameter
seems (to me) to be using templates inappropriately. What's wrong with
denormalised_rational<int>? Or just making a decision on behalf of the
user?
The standard library doesn't provide every possible implementation strategy
for map<> or set<> or list<>... For that matter, std::complex<> doesn't let
you choose whether or not you want polar or cartesian representation.
I'm not sure that it overcomplicates the implementation in any
profound way, or is indeed a misuse of template parameters. Provide a
private member function:
void dependent_normalize()
{
if(Normalization == normalized)
normalize();
}
And in each member function that currently calls normalize(), call
dependent_normalize() instead. This would be compiled in at the same
cost as before for normalized and at no cost for denormalized. Anyway,
just a thought.
I'm not sure that comparisons with the existing standard library hold
up all that well -- consider the joy that is allocators ;->
Kevlin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk