Template Specialization
🔴 Advanced
📖 Definition
Template specialization (template<> class Box<bool>) allows customizing a template's implementation specifically for a particular type or combination of types (e.g. std::vector<bool> space-optimized bitset specialization).
💡 Code Example
#include <iostream>
template <typename T>
struct Formatter {
static void print(T val) { std::cout << "Generic: " << val << "\n"; }
};
// Full specialization for bool
template <>
struct Formatter<bool> {
static void print(bool val) { std::cout << "Bool Spec: " << (val ? "true" : "false") << "\n"; }
};
int main() {
Formatter<int>::print(10);
Formatter<bool>::print(true);
return 0;
}