C++ में Template Specialization
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Explicit Class Specialization
आप एक पूरे class template को भी specialize कर सकते हैं, जो आपको एक specific type के लिए tailored पूरी तरह अलग member variables और functions design करने देता है — useful जब generic implementation उस particular case के लिए correct लेकिन inefficient हो।
उदाहरण: Explicit Class Specialization
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Template Definitions Scope करना
अपनी source files में specialized template definitions को उनके general template counterparts के पास group करके रखें, क्योंकि यह compiler — और future readers — के लिए exactly यह देखना कहीं ज़्यादा आसान बनाता है कि कौन से specialized versions exist करते हैं और हर एक कब apply होता है।
उदाहरण: Scoping Template Definitions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- इसके already इस्तेमाल होने के बाद एक template specialize करना, जो एक error है।
- एक explicit specialization के आगे
template <>भूल जाना। - एक specialization लिखना जिसका signature primary template से match न करे।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: