Template specialization is an advanced feature in C++ that allows you to provide a specific implementation for a particular data type or set of data types, while still maintaining a generic implementation for other types. This feature is particularly useful when you need to optimize or customize the behavior of a template for certain types.
#include <iostream>
#include <type_traits>
template <typename T>
void print_type(T value) {
std::cout << "Generic template: " << value << std::endl;
}
template <>
void print_type<int>(int value) {
std::cout << "Specialized template for int: " << value << std::endl;
}
int main() {
print_type(3.14); // Uses generic template
print_type(42); // Uses specialized template for int
return 0;
}
print_type
that works for any typeint
typeint
is passed, and the generic version for other types#include <iostream>
#include <type_traits>
template <typename T>
class Container {
public:
void store(T value) {
std::cout << "Storing generic value: " << value << std::endl;
}
};
template <>
class Container<bool> {
public:
void store(bool value) {
std::cout << "Storing bool value: " << (value ? "true" : "false") << std::endl;
}
};
int main() {
Container<int> intContainer;
Container<bool> boolContainer;
intContainer.store(42);
boolContainer.store(true);
return 0;
}
Container
class templatebool
typeContainer<bool>
is instantiated#include <iostream>
#include <type_traits>
template <typename T, typename U>
class Pair {
public:
Pair(T first, U second) : first_(first), second_(second) {
std::cout << "Generic Pair" << std::endl;
}
void display() {
std::cout << "First: " << first_ << ", Second: " << second_ << std::endl;
}
private:
T first_;
U second_;
};
template <typename T>
class Pair<T, T> {
public:
Pair(T first, T second) : first_(first), second_(second) {
std::cout << "Specialized Pair for same types" << std::endl;
}
void display() {
std::cout << "Both: " << first_ << ", " << second_ << std::endl;
}
private:
T first_;
T second_;
};
int main() {
Pair<int, double> p1(1, 2.5);
Pair<int, int> p2(3, 4);
p1.display();
p2.display();
return 0;
}
Pair
class template with two type parameters#include <iostream>
#include <type_traits>
template <typename T, typename = void>
struct is_printable : std::false_type {};
template <typename T>
struct is_printable<T, std::void_t<decltype(std::cout << std::declval<T>())>> : std::true_type {};
template <typename T>
void print(const T& value) {
if constexpr (is_printable<T>::value) {
std::cout << "Printing: " << value << std::endl;
} else {
std::cout << "Cannot print this type" << std::endl;
}
}
struct NonPrintable {};
int main() {
print(42);
print("Hello");
print(NonPrintable{});
return 0;
}
is_printable
to check if a type can be printedprint
function uses if constexpr
to choose the appropriate behavior at compile-timeTemplate specialization in C++ is a powerful feature that allows for type-specific optimizations and customizations within generic code. It enables developers to provide tailored implementations for certain types while maintaining a generic version for others. This technique is particularly useful for improving performance, handling special cases, or providing different behaviors based on type characteristics.
By using full specialization, partial specialization (for class templates), and techniques like SFINAE, developers can create flexible and efficient template-based code. However, it's important to use specialization judiciously, as overuse can lead to code bloat and increased compilation times. When used appropriately, template specialization can significantly enhance the expressiveness and efficiency of C++ code, especially in library development and generic programming scenarios.
[1] https://www.modernescpp.com/index.php/template-specialization/ [2] http://www.gotw.ca/publications/mill17.htm [3] https://stackoverflow.com/questions/2197141/function-template-specialization-importance-and-necessity [4] https://www.codeproject.com/Articles/5340890/An-Introduction-to-Cplusplus-Concepts-for-Template [5] https://www.geeksforgeeks.org/template-specialization-c/
Previous Page | Course Schedule | Course Content