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

C++ में Template Specialization

Template specialization एक particular type के लिए एक template का special version देना है, एक dish के लिए एक custom recipe की तरह। बाकी सबके लिए general recipe इस्तेमाल होती है।
Syntax
cpp
template <typename T>
void function_name(T value) { }  // general version

template <>
void function_name<specific_type>(specific_type value) { }  // specialization

Template Specialization क्या है?

Template specialization आपको एक particular data type के लिए एक custom, hand-optimized implementation provide करने देता है, जो generic template specifically उस type के लिए अन्यथा generate करता वह override करते हुए।

उदाहरण: 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

एक function template को explicitly specialize करने के लिए, आप function signature से ठीक पहले 'template <>' लिखते हैं और function नाम के बाद angle brackets में target type specify करते हैं, compiler को बताते हुए कि यह version generic वाले को सिर्फ उस exact type के लिए replace करता है।

उदाहरण: 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

आप एक पूरे class template को भी specialize कर सकते हैं, जो आपको एक specific type के लिए tailored पूरी तरह अलग member variables और functions design करने देता है — useful जब generic implementation उस particular case के लिए correct लेकिन inefficient हो।

उदाहरण: 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;
}

Template Definitions Scope करना

अपनी source files में specialized template definitions को उनके general template counterparts के पास group करके रखें, क्योंकि यह compiler — और future readers — के लिए exactly यह देखना कहीं ज़्यादा आसान बनाता है कि कौन से specialized versions exist करते हैं और हर एक कब apply होता है।

उदाहरण: 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;
}

Code Bloat रोकना

क्योंकि एक template इसके इस्तेमाल हर distinct type के लिए अलग compiled code generate करता है, heavy template use binary size bloat कर सकता है; specialization यहाँ actually मदद कर सकता है कई similar types को एक hand-written, ज़्यादा compact implementation share करने देकर।

उदाहरण: 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. #}
आम गलतियां
  1. इसके already इस्तेमाल होने के बाद एक template specialize करना, जो एक error है।
  2. एक explicit specialization के आगे template <> भूल जाना।
  3. एक specialization लिखना जिसका signature primary template से match न करे।
🔒

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.