Template's functions partial specialization

It is possible to obtain the partial specialization of function template?
How to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<class T, class U, int I> 
void write(T a)
{ 
	cout << "Primary template" << endl; 
};

template<class T, int I> 
void write <T, T*, I> (T a) /*/ U = T* /*/
{ 
	cout << "Partial specialization 1" << endl;
};

template<class T, class U, int I> 
void write <T*, U, I> (T a) /*/ T = T* /*/
{ 
	cout << "Partial specialization 2" << endl;
};

template<class T> 
void write <int, T*, 10> (T a) /*/ T = int, U = T*, I = 10 /*/
{ 
	cout << "Partial specialization 3" << endl; 
};

template<class T, class U, int I> 
void write <T, U*, I> (T a) /*/ U = U* /*/
{ 
	cout << "Partial specialization 4" << endl; 
};
A function template can only be fully specialized, but because function templates can overload we can get nearly the same effect via overloading that we could have got via partial specialization
...
If you're writing a function template, prefer to write it as a single function template that should never be specialized or overloaded, and implement the function template entirely in terms of a class template. This is the proverbial level of indirection that steers you well clear of the limitations and dark corners of function templates. This way, programmers using your template will be able to partially specialize and explicitly specialize the class template to their heart's content without affecting the expected operation of the function template. This avoids both the limitation that function templates can't be partially specialized, and the sometimes surprising effect that function template specializations don't overload. Problem solved.

If you're using someone else's plain old function template (one that's not implemented in terms of a class template as suggested above), and you want to write your own special-case version that should participate in overloading, don't make it a specialization; just make it an overloaded function with the same signature.
- http://www.gotw.ca/publications/mill17.htm
Topic archived. No new replies allowed.