Boost logo

Boost :

From: Alan Griffiths (alan_at_[hidden])
Date: 1999-12-12 07:20:23


In message <flk8mmbft9.fsf_at_[hidden]>, Gabriel Dos Reis
<dosreis_at_[hidden]> writes
>Darin Adler <darin_at_[hidden]> writes:
>
>| > | There is a std::abs() template function, for the complex class.
>| > | Therefore it is both legal to put the rational abs() function in the
>| > ^^^^^
>| > | std namespace and highly recommended since a generic algorithm should
>| > | be able to use std::abs() on any numeric type including user defined
>| > | types.
>| >
>| > 'legal' on which basis? If it is by the Standard, the chapter and verse.

If I am trying to produce a version (carefully avoiding "specialisation"
or "overloading" - those are implementation details) of a standard
entity (e.g. std::swap, or std::abs) for a class of mine the logical
namespace in which to place it is std.

My view is that this is where it _should_ go - 'legal' or not. (The
fact that it appears to be illegal suggests that a defect report is the
appropriate action.)

Playing games with name lookup in the hope of getting the right function
runs into problems both with current compiler technology (you know who I
mean), and with user containing qualified names. N.B. there are
circumstances that require qualified names.

>| 17.4.3.1/1: "It is undefined for a C++ program to add declarations or
>| definitions to namespace std or namespaces within namespace std unless
>| otherwise specified. A program may add template specializations for any
>| standard library template to namespace std. Such a specialization (complete
>| or partial) of a standard library template results in undefined behavior
>| unless the declaration depends on a user-defined name of external linkage
>| and unless the specialization meets the standard library requirements for
>| the original template."
>|
>| Programs are definitely allowed (and recommended) to specialize function
>| templates like std::swap and std::less; in fact my recent change to
>| smart_ptr.hpp does just that.
>
>Stop. You can't specialize a template function. You *overload* it. Two
>differents things; althought they might be seen as having similar
>effects.
>
>| But it's a bit less clear for std::abs, which
>| is the name for both some non-template functions in the standard and for a
>| template (for complex).
>|
>| Kevlin Henney pointed out that, "The abs for complex is in terms of
>| complex<T> rather than simply just T, and so although you would be
>| permitted to specialise abs for complex< rational<T> > this does not give
>| you general licence to specialise for rational<T>."
>
>Right. And right now there is no strong reasons to put it in
>namespace std, apart from (probably) to make it look like standard :)

A little contrived, but not unreasonable...

    template<typename number_type>
    struct example
    {
        static number_type abs(number_type x)
        {
            return std::abs(x);
        }
    };

-- 
Alan Griffiths  (alan_at_[hidden])  http://www.octopull.demon.co.uk/
ACCU Chairman   (chair_at_[hidden])             http://www.accu.org/

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