
Boost : 
From: Boris Fomitchev (fbp_at_[hidden])
Date: 19991214 19:09:55
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
// userdefined 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
}
If you need more of suct tricks, take a look at STLport (www.stlport.org).
There, I often had to resolve this problem in same namespace, which even required
proxy functions to be deployed.
Best regards,
Boris.
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 userdefined 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 "integerlike" semantics, but it
> blows any hope of optimisation for a userdefuined IntType right out of the
> water. (If someone can offer me a suitable implementation for
> standardconforming 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. http://clickhere.egroups.com/click/1762
>
>  Talk to your group with your own voice!
>  http://www.egroups.com/VoiceChatPage?listName=boost&m=1
>
> 
> Name: rational.hpp
> rational.hpp Type: C++ Header File (application/xunknowncontenttypehppfile)
> Encoding: quotedprintable
>
> Name: rational_example.cpp
> rational_example.cpp Type: C++ Source File (application/xunknowncontenttypecppfile)
> Encoding: 7bit
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk