Boost logo

Boost :

From: John Maddock (John_Maddock_at_[hidden])
Date: 2000-05-16 06:36:56

>Specifically, there's no <limits>, and boost/operators.hpp gives "no class
template named 'iterator' in 'std'" !!! [Plus a warning about friend
templates, which may be because GCC doesn't support them - anyone know?]

Try adding the SGI STL to your include path, alternatively you could try
installing either the STLPort, or the latest beta of the upcoming full
SGI/GNU standard library. That fixes all the problems except the friend
function - I see an unresolved external at link time for this - and that is
correct behaviour, currently you have:

    friend rational abs(const rational& r);

which declares a non-template friend function, this is *not* the same as
the template function you declare later:

template <typename IntType>
inline rational<IntType> abs(const rational<IntType>& r)
    // We want to use abs() unadorned below, so that if IntType is a
    // user-defined type, the name lookup rules will work to find an
    // appropriate abs() defined for IntType.
    // We protect this with BOOST_NO_STDC_NAMESPACE, as some compilers
    // don't put abs into std:: (notably MS Visual C++) and so we can't
    // "use" it from there.
    using std::abs;
    // Note that with normalized fractions, the denominator is always
    return rational<IntType>(abs(r.num), r.den);

gcc issues the warnings because it knows that the non-inline friend
declaration is probably an error, to be correct there would have to be a
non-template function abs for each instantiation of rational:

inline rational<int> abs(const rational<int>& r)
    return rational<int>(std::abs(r.num), r.den);

// etc

The easiest solution in this case seems to be to make abs a non-friend
(this solves the problems that C++ Builder has also):

template <typename IntType>
inline rational<IntType> abs(const rational<IntType>& r)
    using std::abs;
    return rational<IntType>(abs(r.numerator()), r.denominator());

The use of accessor functions here probably shouldn't affect performance at
all, and this removes the need to declare this a friend, although if that's
what you wanted the syntax would be something like:

friend rational abs<IntType>(rational r);

But only a few compilers currently support this with any reliability.

Finally, have you considered adding a numeric_limits specialisation? I
have an idea what it should look like if you're interested.

- John.

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