C++ Template Specialization
In this page:
template <typename T>
void function_name(T value) { } // general version
template <>
void function_name<specific_type>(specific_type value) { } // specialization
What is Template Specialization?
Template specialization lets you provide a custom, hand-optimized implementation for one particular data type, overriding what the generic template would otherwise generate for that type specifically.
Example: What is Template Specialization?
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
void show(T value) {
// Print to the console with cout
std::cout << "Generic: " << value << std::endl;
}
// Program execution starts in main()
int main() {
show(5);
show(3.14);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Explicit Function Specialization
To specialize a function template explicitly, you write 'template <>' immediately before the function signature and specify the target type after the function name in angle brackets, telling the compiler this version replaces the generic one only for that exact type.
Example: Explicit Function Specialization
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
void show(T value) {
// Print to the console with cout
std::cout << "Generic: " << value << std::endl;
}
// Declare a template so this works with any suitable type
template <>
void show<char>(char value) {
// Print to the console with cout
std::cout << "Character: " << value << std::endl;
}
// Program execution starts in main()
int main() {
show(5);
show('A');
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Explicit Class Specialization
You can specialize an entire class template as well, which lets you design completely different member variables and functions tailored to one specific type — useful when the generic implementation would be correct but inefficient for that particular case.
Example: Explicit Class Specialization
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
// Define the type Printer
class Printer {
// Members below can be accessed from outside the class
public:
void print(T value) { std::cout << "Value: " << value << std::endl; }
};
// Declare a template so this works with any suitable type
template <>
class Printer<bool> {
// Members below can be accessed from outside the class
public:
void print(bool value) { std::cout << (value ? "true" : "false") << std::endl; }
};
// Program execution starts in main()
int main() {
Printer<int> p1;
p1.print(5);
Printer<bool> p2;
p2.print(true);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Scoping Template Definitions
Keep specialized template definitions grouped near their general template counterparts in your source files, since this makes it much easier for the compiler — and for future readers — to see exactly which specialized versions exist and when each one applies.
Example: Scoping Template Definitions
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
void show(T value) {
// Print to the console with cout
std::cout << "Generic: " << value << std::endl;
}
// Declare a template so this works with any suitable type
template <>
void show<int>(int value) {
// Print to the console with cout
std::cout << "Int: " << value << std::endl;
}
// Program execution starts in main()
int main() {
show(5);
show(3.14);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Preventing Code Bloat
Because a template generates separate compiled code for every distinct type it's used with, heavy template use can bloat binary size; specialization can actually help here by letting several similar types share one hand-written, more compact implementation.
Example: Preventing Code Bloat
#include <iostream>
template <typename T>
void process(T value) {
std::cout << value << std::endl;
}
int main() {
// Each distinct type used (int, double, ...) generates its own compiled
// copy; specialization can share logic across similar types instead.
process(5);
process(3.14);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Specializing a template after it has already been used, which is an error.
- Forgetting
template <>in front of an explicit specialization. - Writing a specialization whose signature does not match the primary template.
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: