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

C++ Templates का परिचय

Templates आपको एक बार code लिखने देते हैं जो कई types के लिए काम करे, किसी भी dough काट सकने वाले एक cookie cutter की तरह। Compiler आपको चाहिए होने वाला version बनाता है।
Syntax
cpp
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?

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

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

Code Reusability

Templates के बिना, आपको हर type के लिए जिसे आप support करना चाहें लगभग identical overloaded functions hand-write करने होंगे — int के लिए एक, float के लिए एक, double के लिए एक।

एक template इन सबको एक definition में collapse कर देता है जिसे compiler automatically जिस भी type के साथ आप इसे इस्तेमाल करें उसके लिए specialize कर देता है।

उदाहरण: Code Reusability

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

Template Parameters

एक template के angle brackets के अंदर, typename या class keyword एक type के लिए एक placeholder introduce करता है, जिसे compiler आपके दिए (या इसके infer किए) real type से replace करता है जहाँ भी वह placeholder template की body में appear होता है।

उदाहरण: Template Parameters

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

Compile-Time Generation

Templates compile time पर instantiate होते हैं: compiler उन types को देखता है जिनके साथ आप actually एक template इस्तेमाल करते हैं और हर एक के लिए उस function या class का एक distinct, पूरी तरह optimized version generate करता है — genericity से खुद कोई runtime overhead नहीं है।

उदाहरण: Compile-Time Generation

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(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;
}

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

cpp
#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;
}
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. template definitions को एक .cpp file में रखना और linker errors पाना, क्योंकि templates आमतौर पर headers में belong करते हैं।
  2. definition से पहले template <typename T> भूल जाना।
  3. यह उम्मीद करना कि एक template एक बार compile होगा और किसी भी type के लिए काम करेगा, जब यह इस्तेमाल किए हर type के लिए अलग instantiate होता है।
🔒

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.