skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
cpp-partial-specialization
This page is read only. You can view the source, but not change it.
# C++ partial specialization **[Partial specialization](https://en.cppreference.com/w/cpp/language/partial_specialization)** allows template classes (not functions) to define specialized versions for particular template parameter patterns. For example, a template class can have a separate specialization for pointer types or for template types, enabling different implementations for different type categories. Use partial specialization to provide optimized or behavior-specific implementations for certain template argument patterns. ## Example This example shows partial specialization for pointers and containers. ```cpp // compile: g++ -std=c++17 -o partial partial.cpp // run: ./partial // description: partial specialization customizes templates for parameter patterns #include <iostream> #include <vector> // Primary template template <typename T> class Printer { public: void print(const T& x) { std::cout << "Generic\n"; } }; // Partial specialization for pointers template <typename T> class Printer<T*> { public: void print(T* const& x) { std::cout << "Pointer specialization\n"; } }; // Partial specialization for containers template <typename T> class Printer<std::vector<T>> { public: void print(const std::vector<T>& x) { std::cout << "Vector specialization\n"; } }; int main() { Printer<int> p1; p1.print(42); Printer<int*> p2; int x = 10; p2.print(&x); Printer<std::vector<int>> p3; std::vector<int> v{1, 2, 3}; p3.print(v); return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top