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

C++ के Function Templates

एक function template एक recipe है जो कई types के लिए काम करती है, numbers, letters या words compare कर सकने वाली एक machine की तरह। आप इसे type के लिए एक blank के साथ एक बार लिखते हैं।
Syntax
cpp
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

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

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

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

कई Template Parameters

एक single function template कई type parameters declare कर सकता है, एक generic function को same call में genuinely अलग, unrelated types के कई arguments handle करने देते हुए उन्हें सबको match करने की ज़रूरत की बजाय।

उदाहरण: Multiple Template Parameters

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

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

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

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

cpp
// 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;
}
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. max(3, 4.5) जैसे अलग types के arguments pass करना और एक deduction error पाना, क्योंकि दोनों को same T देना ज़रूरी है।
  2. function के ऊपर template <typename T> भूल जाना।
  3. एक ऐसा operation इस्तेमाल करना जिसे type support नहीं करता, जैसे इसके बिना एक type पर <।
🔒

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.