Boost logo

Boost :

From: Stephen Silver (boost_at_[hidden])
Date: 2001-01-13 18:19:18


I wrote:

> Paul Moore wrote:

> > 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.

Of course, I was talking nonsense there. The compiler can optimize
out one construct/destruct pair that implicit_cast causes, but there
is a second construct/destruct pair that cannot be removed.

So I suggest this, which simply does the initialization directly,
but will not compile if the conversion cannot also be done by
assignment:

    template <class T> rational(const T& n)
        : num(n), den(1) { if (0) { num = n; } }

Or, in view of Paul's difficulty getting the template to compile
with MSVC++, perhaps the definition should be:

#ifndef BOOST_MSVC
    template <class T> rational(const T& n)
        : num(n), den(1) { if (0) { num = n; } }
#endif

and MSVC++ users will simply have to use casts where necessary...

Stephen


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