Quick reference guide for template declarations Tuesday 29th January 2008
Function templates by example
Your basic template declaration. (No angle brackets after the template name.)
template< class T, class U > T* just_template( U );
Declaration of explicit (full) specialization:
template<> char* just_template< char, int >( int );
Explicit instantiation. (No angle brackets after the template keyword.)
template const char* just_template< const char, double >( double );
Class templates by example
Basic declaration:
template< class T > class YetAnotherTemplate;
Declaration of partial specialization (e.g. for pointer types):
template< class T > class YetAnotherTemplate< T* >;
Declaration of full specialization (for long):
template<> class YetAnotherTemplate< long >;
Explicit instantiation:
template class YetAnotherTemplate< float >;
Notes
You can’t partially specialize function templates. This is a good thing(tm) in my opinion. For a better explanation than I can give, see Sutter’s Mill.
Partial specialization doesn’t necessarily mean fewer template parameters, it really does just mean more specialized.
For a function template specialization you can omit any trailing template arguments that can be deduced; if all template arguments can be deduced you can just use the unadorned template name in the declaration of the specialization.
Similarly, for explicit instantiations of function templates you can just use the template name where the arguments can be deduced.
e.g.
template< class T > void f( T ); template f( int ); // Explicit instantiation template<> f( double ); // Declaration of explicit specialization
Maybe function templates are just a bad idea.