Boost logo

Boost :

From: Kevlin Henney (Kevlin.Henney_at_[hidden])
Date: 1999-12-09 08:14:58


> It struck me that my rational number class could do with a std::abs()
> implementation. What is the accepted way to do this? Should I do
[snip boost::abs]
> or should I put my abs() into namespace std, using
[snip boost::abs]
> I sort of lean towards the latter, as code like
>
> using std::abs;
> using boost::rational;
> int n = -1;
> rational<int> r(-1);
> n = abs(n);
> r = abs(r);

> seems natural to me. Requiring an additional "using boost::abs" seems
> unintuitive. But I'm not sure if it is legitimate to add overloads in the
> std:: namespace like this... I couldn't find any examples of things like
> this which I could learn from (std::swap specialisations seem to be
> implemented as member functions).

     In most examples Koenig Lookup will ensure that either approach is
     transparent as the relevant abs will be searched for in the namespace
     from which rational comes, ie boost, as well as anywhere else
     specified, ie std.

     However, one use that would fall foul of expectation would be full
     explicit qualification:

          r = std::abs(r);

     This suggests that abs should be in std. However, my reading of the
     standard says that only template specialisations of library templates
     are permitted in std; the abs for rational is a separate overload
     rather than a specialisation, so I think that such an overload would
     not be permitted. Can anyone confirm my reading of this?

     Kevlin


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