C++ Templates का परिचय
In this page:
template <typename T>
return_type function_name(T param) {
// works with any type T
}
template <typename T>
class ClassName {
T member;
};
Templates क्या हैं?
Templates आपको C++ में genuinely generic code लिखने देते हैं — एक single class या function definition जो essentially किसी भी data type के साथ काम कर सकता है, compiler पीछे से आपको actually चाहिए होने वाला type-specific version generate करता है।
उदाहरण: What are Templates?
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
T maxValue(T a, T b) {
// Send (a > b) ? a : b back to the caller
return (a > b) ? a : b;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << maxValue(3, 7) << std::endl;
// Print to the console with cout
std::cout << maxValue(3.5, 2.5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Code Reusability
Templates के बिना, आपको हर type के लिए जिसे आप support करना चाहें लगभग identical overloaded functions hand-write करने होंगे — int के लिए एक, float के लिए एक, double के लिए एक।
एक template इन सबको एक definition में collapse कर देता है जिसे compiler automatically जिस भी type के साथ आप इसे इस्तेमाल करें उसके लिए specialize कर देता है।
उदाहरण: Code Reusability
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
T add(T a, T b) {
// Send a + b back to the caller
return a + b;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << add(2, 3) << std::endl;
// Print to the console with cout
std::cout << add(2.5, 3.5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Template Parameters
एक template के angle brackets के अंदर, typename या class keyword एक type के लिए एक placeholder introduce करता है, जिसे compiler आपके दिए (या इसके infer किए) real type से replace करता है जहाँ भी वह placeholder template की body में appear होता है।
उदाहरण: Template Parameters
// 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 << value << std::endl;
}
// Program execution starts in main()
int main() {
show(5);
show("hello");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compile-Time Generation
Templates compile time पर instantiate होते हैं: compiler उन types को देखता है जिनके साथ आप actually एक template इस्तेमाल करते हैं और हर एक के लिए उस function या class का एक distinct, पूरी तरह optimized version generate करता है — genericity से खुद कोई runtime overhead नहीं है।
उदाहरण: Compile-Time Generation
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
T square(T x) {
// Send x * x back to the caller
return x * x;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << square(4) << std::endl;
// Print to the console with cout
std::cout << square(2.5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Templates के साथ Type Safety
void pointers या preprocessor macros जैसी पुरानी techniques के उलट, templates को instantiation के point पर compiler से strictly type-checked किया जाता है, type errors को compilation के दौरान पकड़ते हुए इसकी बजाय कि उन्हें runtime पर program crash करने के लिए slip करने दिया जाए।
उदाहरण: Type Safety with Templates
#include <iostream>
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
std::cout << add(3, 4) << std::endl;
// add(3, "text"); would fail to compile: type mismatch caught at compile time
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- template definitions को एक
.cppfile में रखना और linker errors पाना, क्योंकि templates आमतौर पर headers में belong करते हैं। - definition से पहले
template <typename T>भूल जाना। - यह उम्मीद करना कि एक template एक बार compile होगा और किसी भी type के लिए काम करेगा, जब यह इस्तेमाल किए हर type के लिए अलग instantiate होता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: