//generic template template <typename T> class vector { // accessor functions and so forth private: T* vec_data; // we'll store the data as block of dynamically allocated // memory int length; // number of elements used int vec_size; // actual size of vec_data }; //specialized template <> class vector <bool> { // interface private: unsigned int *vector_data; int length; int size; };
Besides, the interface for this specialized interface can be totally different from the generic template: they can have different methods. In some sense (or scenarios), template specialization can implement (substitute) the functionality of virtual inheritance. The major drawback of template specialization is increased code size and resulting complexity.
Similar to template specialization, we can also have partial specialization, which means you specialize certain features but still leave some feature for users to choose. The following shows one such example.
//generic template template <typename T, unsigned length> class fixedVector { ... }; //partial specialization template <unsigned length> class fixedVector <bool, length> { ... };
As we can see here, in the partial specialization, the vector is determined to contain the bool type, but the length can still be specified.