Subject: Re: Shades, swap or whatever
From: "Peter Dimov" <[email-masked]>
To: "David Abrahams" <[email-masked]>, "Griffiths, Alan" <[email-masked]>
Date: Wed, 1 Nov 2000 17:01:18 +0200

> We recognized the problem, then spent a bunch of time trying to figure
out
> why partial specialization might be better than allowing overloading.
> Eventually, we came across this case posted by Peter Dimov to
c.l.c++.m:
>
> template <class T> void f(T);  // #1
> template <> void f<int*>(int*);  // #2
> template <class T> void f(T*);   // #3
>
> int *x;
> f(x); // which one gets called? #3, surprisingly

To clarify, you felt that this example is an argument in favour of
allowing std:: overloading and not allowing partial specializations?

> It seems to me that the confusion comes from mixing overloading and
> specialization. The two mechanisms don't play well together. Since IMO
few
> people have been writing function specializations anyway, and it's a
problem
> we can solve entirely in the LWG without a change to the core
language, it
> would be better to allow overloading in std and discourage
specialization of
> function templates.

But note that allowing

struct base {};

namespace std
{
    void swap(base &, base &);
}

would let the following compile, if I'm not mistaken:

struct derived: base {};

base b;
derived d;

std::swap(b, d);

An overload in this case cannot satisfy 17.4.3.1/1, "meets the standard
library requirements for the original template" - the original template
does not work for different types.

This is the primary advantage of specializations (partial or explicit)
over overloads - they have exactly the same interface as the original
template.

> Core has also discussed this. Some in core thought that it was an
obvious
> omission that partial specialization of function templates was not
allowed,
> but eventually they had a problem with this particular proposal
because it
> was not always clear from the proposed syntax exactly what might be
> specialized.

Did they show a concrete example of an ambiguity? I was pretty certain
at the time that a partial specialization declaration can never be
ambiguous.

--
Peter Dimov
Multi Media Ltd.