📘 Lesson · Lesson 73
Templates
त्वरित सार
Template किसी भी type के लिए एक बार code लिखता है। यह पृष्ठ अधिक उन्नत रूपों के लिए एक संक्षिप्त syntax reference है; concept, function और class templates, और STL इन्हें कैसे इस्तेमाल करती है, इसके लिए मुख्य templates lesson देखें।
Template specialization
किसी एक type को template <> से उसका अपना version दें:
C++
template <typename T>
T doubleIt(T x) { return x + x; }
template <> // string ke liye specialization
string doubleIt(string s) { return s + s; }
// doubleIt(3) → 6 ; doubleIt(string("ab")) → "abab"Non-type parameters
Template parameter एक compile-time value हो सकता है, केवल type नहीं:
C++
template <typename T, int N>
class Array {
T data[N]; // size compile time par fixed
public:
int size() { return N; }
};
// Array<int, 5> a; → a.size() == 5Default template arguments
C++
template <typename T = int>
class Holder {
T value;
};
// Holder<> h; → default int istemaalCall पर explicit type
जब deduction संभव न हो या आप विशिष्ट type चाहें, इसे angle brackets में नाम दें:
C++
cout << maxOf<double>(3, 4.5); // T = double force karein
Syntax pitfalls
- Template definitions आमतौर पर header files में होती हैं, ताकि compiler इन्हें जहाँ भी इस्तेमाल हों instantiate कर सके।
- पुराने compilers को
vector<vector<int> >में space चाहिए था; आधुनिक C++>>स्वीकारता है। - Template के अंदर dependent type से पहले
typenameज़रूरी है, जैसेtypename T::iterator।
आम गलतियाँ
- Template की definition .cpp file में रखना और linker errors पाना।
- पूरे specialization पर
template <>भूलना। - जहाँ non-type parameter को compile-time constant चाहिए वहाँ runtime variable भेजना।
- Dependent type से पहले
typenameछोड़ना।
🏋️ अभ्यास
एक class template Stack<T, int N> लिखें जिसकी fixed capacity non-type parameter से हो। push और pop जोड़ें, और bool के लिए print specialize करें ताकि true/false दिखे।
सारांश
- Specialization किसी एक type को
template <>से custom version देता है। - Non-type parameters sizes जैसी compile-time values भेजते हैं।
- Template parameters के default arguments हो सकते हैं।
- जब deduction काफ़ी न हो, angle brackets में type बताएँ।
- Templates headers में परिभाषित करें; concept के लिए मुख्य templates lesson देखें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में template specialization क्या है?
Template specialization आपको किसी एक विशिष्ट type के लिए template का अलग implementation देने देता है। सामान्य template अधिकांश types संभालता है, जबकि specialization, concrete type के साथ
template <> लिखा, उस मामले के लिए custom व्यवहार देता है, जैसे bool को अलग मानना।C++ में non-type template parameter क्या है?
Non-type template parameter एक compile-time value है, type के बजाय, जो template को भेजा जाता है — आमतौर पर एक integer। जैसे
template <typename T, int N> किसी class template को compile time पर array size N तय करने देता है, जैसे std::array करता है।क्या C++ में template parameters के default values हो सकते हैं?
हाँ। Template parameters के defaults हो सकते हैं,
template <typename T = int> की तरह लिखे, तो आप argument छोड़ सकते हैं और default पा सकते हैं। यह standard library में आम है, जहाँ containers अपने allocator और अन्य policy parameters को default करते हैं।C++ में template type स्पष्ट रूप से कैसे बताते हैं?
आप नाम के बाद angle brackets में type रखते हैं, जैसे
maxOf<double>(3, 4) या Box<int>। Function templates के लिए यह केवल तब चाहिए जब compiler arguments से type न निकाल सके; class templates के लिए यह आमतौर पर ज़रूरी है।C++ में template definitions आमतौर पर header files में क्यों जानी चाहिए?
क्योंकि compiler को इस्तेमाल की जगह हर type के लिए instantiate करने को पूरी template definition चाहिए, templates आमतौर पर अलग source files के बजाय headers में परिभाषित होते हैं। Template की declaration और definition को translation units में बाँटना अक्सर linker errors पैदा करता है।
