11

I read something that it might be confusing for the compiler to write

template <class T>
void calculator<std::complex<T>>::myMin();

but maybe just give it a hint like so? To make it clear that it is a partial specialization.

template < , class T>
void calculator<std::complex<T>>::myMin();
E_net4
  • 27,810
  • 13
  • 101
  • 139
Hakaishin
  • 2,550
  • 3
  • 27
  • 45

2 Answers2

10

From http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#229 linked by @danh in the comments above:

Notes from 10/00 meeting:

A major concern over the idea of partial specialization of function templates is that function templates can be overloaded, unlike class templates. Simply naming the function template in the specialization, as is done for class specialization, is not adequate to identify the template being specialized.

fwyzard
  • 2,364
  • 1
  • 21
  • 19
  • Why you make an already given comment from 2017! as a new answer? – Klaus May 17 '21 at 07:25
  • 10
    Because not everyone may feel like following the link and looking for the relevant piece of text in that linked document. – fwyzard May 18 '21 at 08:06
2

Why does the C++ standard not allow function template partial specialization?

Because, reverse your thinking, what would happen if the Standard allows template function to be partially specialized?

Suppose this simple example assuming the Standard allows partial specialization of template functions:

template<class T> void f( T ) { print("A-overload") };  
template<class T> void f( T* ){ print("B-overload") };
 
// template<class T> void f(T*) { print("A-partial-specialization") };

Can you now differentiate between the B-overload and partial specialization of A? Indeed you can't!

Moreover, suppose you have another overload that takes T** and a partial specialization from overload-A or overload-B with exactly the same signature, what will you do?

In general, The notion of partial specialization only exists for class templates (described by §14.5.5) and member templates (i.e. members of a template class that are themselves template functions, described by §14.5.5.3/2). It does not exist for ordinary members of class templates, nor does it exist for function templates – simply because it is not described by the Standard.

mada
  • 1,646
  • 1
  • 15