Partial template specialization
From Wikipedia, the free encyclopedia
Partial template specialization is a method of optimizing generic code at compile time. Usually used in reference to the C++ language, it allows the programmer to specialize only some of a template class's parameters.
[edit] Templates and specialization
Templates are really meta-classes: they are partial abstract data types that provide instructions to the compiler on how to create classes with the proper data members. For example, a vector, a dynamic array, is a template. When a programmer uses a vector, one instantiates it with a specific data type, for example, int, string or double. Each type of vector results in a different class in the compiler's object code, each one working with a different data type.
If one knows that a template class will be used with a specific data type fairly often and this data type allows some optimizations (e.g. bit shifting with integers, as opposed to multiplying or dividing by 2), one may specialize the template by specifying another template class that is identical but by specifying the parameter types. When the compiler sees such a template class instantiated in code, it uses the specialized version instead of generating its own.
[edit] Partial specialization
Now take a template class with two parameter types, for example, STL's map class. Most compilers only allow one to specialize all or none of the template's parameters. Ones that allow partial specialization allow the programmer to specialize one parameter while leaving the other generic. Until recently most compilers did not support this, and as of 2006 most do not support the feature well (at best) or ignore such specializations (at worst).