C++ Template Specialization
Template specialization means writing a custom version of a template for a specific type. The general template handles most types, while the specialized version handles one type differently.
This is useful when one type needs special logic, formatting, or storage rules, but you still want the same template name for all types.
Why Specialize a Template?
A template gives you reusable code, but not every type behaves the same way. For example, printing an int might be straightforward, while printing a bool as true or false may be clearer than printing 1 or 0.
Specialization lets you keep the normal template for the common case and replace only the part that needs different behavior.
Function Template Specialization
An explicit specialization of a function template starts with template <>. Then you write the same function name with the specific type you want to handle.
The general template is used unless the call exactly matches a specialization.
Class Template Specialization
Class templates can also be specialized. A specialized class can have different member variables, different functions, or different output from the general template.
The syntax is similar: write template <>, then define the class with the chosen type in angle brackets, such as Storage<bool>.
Example
This program uses a general function template and a specialized version for bool. It also specializes a small Storage class template for bool.
#include <iostream>
#include <string>
using namespace std;
template <typename T>
void showValue(T value) {
cout << "Value: " << value << endl;
}
template <>
void showValue<bool>(bool value) {
cout << "Value: " << (value ? "true" : "false") << endl;
}
template <typename T>
class Storage {
private:
T value;
public:
Storage(T start) : value(start) {}
void print() const {
cout << "Stored item: " << value << endl;
}
};
template <>
class Storage<bool> {
private:
bool value;
public:
Storage(bool start) : value(start) {}
void print() const {
cout << "Stored flag: " << (value ? "on" : "off") << endl;
}
};
int main() {
showValue(42);
showValue(string("hello"));
showValue(true);
Storage<int> score(95);
Storage<bool> enabled(false);
score.print();
enabled.print();
return 0;
}
Output:
Value: 42
Value: hello
Value: true
Stored item: 95
Stored flag: off
How the Example Works
The first showValue is the general template. It works for values that can be printed with cout <<. The specialized showValue<bool> version is chosen when the argument is a bool, so true is printed as text.
The general Storage<T> class prints Stored item:. The specialized Storage<bool> class has its own print function and prints on or off instead.
Full and Partial Specialization
The examples above are full specializations because they replace the template for one exact type: bool. Class templates can also use partial specialization, where only part of the template pattern is specialized, such as pointer types. Function templates cannot be partially specialized; use overloading for that case.
When To Use Specialization
Use specialization when most types should share one template, but one specific type needs a clearly different implementation. Common uses include custom formatting, optimized storage, and special rules for pointer or boolean types.
Do not specialize just to make unrelated behavior share a name. If the logic is completely different, a separate function or class may be easier to read.
Takeaway
Template specialization lets you keep a general template while giving selected types their own exact behavior.
