C++ के Function Templates
In this page:
template <typename T>
T function_name(T param1, T param2) {
return expression;
}
function_name<type>(arg1, arg2); // explicit type
function_name(arg1, arg2); // type deduced
Function Templates Define करना
एक function template एक generic function blueprint define करता है: आप template keyword लिखते हैं जिसके बाद angle brackets में type parameters की एक list, फिर एक normal-looking function signature जो concrete types की जगह उन parameter names इस्तेमाल करता है।
उदाहरण: Defining Function Templates
// 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(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
Function Templates Call करना
आप एक function template को explicitly खुद angle brackets के अंदर type specify करके call कर सकते हैं, या बस इसे एक normal function की तरह call करके compiler को आपके pass किए arguments से automatically type deduce करने दें — practice में latter कहीं ज़्यादा common है।
उदाहरण: Calling Function Templates
// 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<double>(2.5) << std::endl;
// Print to the console with cout
std::cout << square(4) << 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
एक single function template कई type parameters declare कर सकता है, एक generic function को same call में genuinely अलग, unrelated types के कई arguments handle करने देते हुए उन्हें सबको match करने की ज़रूरत की बजाय।
उदाहरण: Multiple Template Parameters
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T1, typename T2>
void showPair(T1 a, T2 b) {
// Print to the console with cout
std::cout << a << " " << b << std::endl;
}
// Program execution starts in main()
int main() {
showPair(5, "hello");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Templates Overload करना
आप एक function template को exactly एक ordinary function की तरह overload कर सकते हैं, या तो एक particular type के लिए एक specific non-template overload provide करके या एक अलग संख्या में parameters वाला दूसरा template define करके, और compiler best match चुनता है।
उदाहरण: Overloading Function 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;
}
int maxValue(int a, int b, int c) {
// Send maxValue(maxValue(a, b), c) back to the caller
return maxValue(maxValue(a, b), c);
}
// 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, 7, 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
Non-Type Template Parameters
Templates non-type parameters भी accept करते हैं — types की बजाय integers जैसी constant values — जिन्हें compiler function body के अंदर compile-time constants की तरह treat करता है, instantiation पर baked in fixed buffer sizes जैसी चीज़ों के लिए useful।
उदाहरण: Non-Type Template Parameters
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <int N>
int multiplyByN(int x) {
// Send x * N back to the caller
return x * N;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << multiplyByN<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
max(3, 4.5)जैसे अलग types के arguments pass करना और एक deduction error पाना, क्योंकि दोनों को sameTदेना ज़रूरी है।- function के ऊपर
template <typename T>भूल जाना। - एक ऐसा operation इस्तेमाल करना जिसे type support नहीं करता, जैसे इसके बिना एक type पर
<।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: