Boost logo

Boost :

From: Beman Dawes (beman_at_[hidden])
Date: 1999-12-10 20:44:35


At 05:05 PM 12/10/99 -0800, Darin Adler wrote:

>Recently, someone pointed out to me that the swap definition in the
above
>code is illegal. I thought that I was specializing a swap function
template,
>but in fact this is not possible. As I understand it, there's no
such thing
>as a specialization of a function template. It's possible to
overload a
>function template, but not specialize it.

Darin, I think you have been misinformed. Here is 14.7 [temp.spec]
paragraph 3:

An explicit specialization may be declared for a function template, a
class template, a member of a class template or a member template. An
explicit specialization declaration is introduced by template<>. In
an explicit specialization declaration for a class template, a member
of a class template or a class member template, the name of the class
that is explicitly specialized shall be a template-id. In the
explicit specialization declaration for a function template or a
member function template, the name of the function or
member function explicitly specialized may be a template-id.

[Example:
 template<class T = int> struct A {
 static int x;
 };
 template<class U> void g(U) { }
 template<> struct A<double> { }; // specialize for T == double
 template<> struct A<> { }; // specialize for T == int
 template<> void g(char) { } // specialize for U == char
 // U is deduced from the parameter type
 template<> void g<int>(int) { } // specialize for U == int
 template<> int A<char>::x = 0; // specialize for T == char
 template<class T = int> struct B {
 static int x;
 };
 template<> int B<>::x = 1; // specialize for T == int
 —end example]

Note two specializations of g(). The standard contains many other
examples of specializations of template functions.

I think the confusion comes because *partial* specialization of
function templates is not permitted.

--Beman


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