|
Boost : |
From: Paul Moore (gustav_at_[hidden])
Date: 1999-12-15 16:33:25
From: Boris Fomitchev [mailto:fbp_at_[hidden]]
>
> Well, abs:: already contains the conditional you need, just make it
> explicit (example below works):
>
> template <typename IntType>
> inline rational<IntType> abs(const rational<IntType>& r)
> {
> #ifndef BOOST_NO_STDC_NAMESPACE
> // 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);
> #else
> // Note that with normalized fractions, the denominator is always
positive.
> return rational<IntType>(::abs(r.num), r.den);
> #endif
> }
Hmm. I wondered about something like this. The problem is that it won't work if
IntType is a user-defined type (specifically, an unlimited precision integer
type). You don't want std::abs then, as std::abs isn't overloaded for my_bigint.
In that case you really do need abs().
But it may be as close as I will get...
Thanks,
Paul
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk