Boost logo

Boost :

From: Boris Fomitchev (fbp_at_[hidden])
Date: 1999-12-14 19:09:55

Well, abs:: already contains the conditional you need, just make it explicit (example below

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.
    // fbp : changed to explicit qualifier
    // using std::abs;
    // Note that with normalized fractions, the denominator is always positive.
    return rational<IntType>(std::abs(r.num), r.den);
    // Note that with normalized fractions, the denominator is always positive.
    return rational<IntType>(::abs(r.num), r.den);

If you need more of suct tricks, take a look at STLport (
There, I often had to resolve this problem in same namespace, which even required
proxy functions to be deployed.

Best regards,

Paul Moore wrote:

> I am still struggling with implementing abs() for rationals.
> To summarise, I have
> template <typename IntType> class rational {
> ...
> friend rational abs (const rational &);
> }
> template <typename IntType>
> inline rational<IntType> abs (const rational<IntType> &r)
> {
> // point (1)
> return rational<IntType>(abs(r.num), r.den);
> }
> Now, Koenig Lookup should find the friend abs when abs(r) is called with a
> rational. But what about the *definition* of abs()? I use abs() here, as using
> std::abs precludes IntType being a user-defined type. But to do this, I need to
> add "using std::abs;" at point (1).
> Now we have an issue - the using declaration needs to be protected by
> BOOST_NO_STDC_NAMESPACE from <boost/config.hpp>.
> But even with this, MSVC *still* has a problem - it fails to look for ::abs
> [[ie, std::abs]] when compiling abs(rational). The error is
> ..\..\boost/rational.hpp(248) : error C2784: 'class boost::rational<IntType>
> __cdecl boost::abs (const class boost::rational<IntType> &)' : could not deduce
> template argument for 'const class boost::rational<IntType> &' from 'const int'
> rational_example.cpp(61) : see reference to function template
> instantiation 'class boost::rational<int> __cdecl boost::abs(const class
> boost::rational<int> &)' being compiled
> which seems to imply that MSVC isn't looking for the "normal" abs(int).
> Can anybody suggest a workaround for this??? I ought to say that MSVC
> compatibility is an absolute requirement - I use MSVC, so I'm not going to write
> code which doesn't work with it (even allowing for the fact that I'm not sure
> exactly what the standard would mandate on this issue!).
> I'm thinking that I might be reduced to replacing abs(n) with (n < 0 ? -n : n).
> This is OK, as we are requiring IntType to have "integer-like" semantics, but it
> blows any hope of optimisation for a user-defuined IntType right out of the
> water. (If someone can offer me a suitable implementation for
> standard-conforming compilers, I'd be willing to put the hack in an ifdef
> _MSC_VER).
> All assistance gratefully accepted :-) I attach the header and test program.
> Paul
> ------------------------------------------------------------------------
> Check out Media Grok and 14 other FREE newsletters from
> The Industry Standard.
> -- Talk to your group with your own voice!
> --
> ------------------------------------------------------------------------
> Name: rational.hpp
> rational.hpp Type: C++ Header File (application/x-unknown-content-type-hppfile)
> Encoding: quoted-printable
> Name: rational_example.cpp
> rational_example.cpp Type: C++ Source File (application/x-unknown-content-type-cppfile)
> Encoding: 7bit

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