← Back to C++ Course | Chapter 11: Templates | Lesson 4 of 5

C++ Template Specialization

Template specialization is giving a special version of a template for one particular type, like a custom recipe for one dish. The general recipe is used for everything else.
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Specializing a template after it has already been used, which is an error.
  2. Forgetting template <> in front of an explicit specialization.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.