Templates
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
Quick recap
A template writes code once for any type. This page is a compact syntax reference for the more advanced forms; for the concept, function and class templates, and how the STL uses them, see the main templates lesson.
Template specialization
Give one type its own version with template <>:
template <typename T>
T doubleIt(T x) { return x + x; }
template <> // specialization for string
string doubleIt(string s) { return s + s; }
// doubleIt(3) → 6 ; doubleIt(string("ab")) → "abab"Non-type parameters
A template parameter can be a compile-time value, not just a type:
template <typename T, int N>
class Array {
T data[N]; // size fixed at compile time
public:
int size() { return N; }
};
// Array<int, 5> a; → a.size() == 5Default template arguments
template <typename T = int>
class Holder {
T value;
};
// Holder<> h; → uses int by defaultExplicit type on a call
When deduction is not possible or you want a specific type, name it in angle brackets:
cout << maxOf<double>(3, 4.5); // force T = double
Syntax pitfalls
- Template definitions usually belong in header files, so the compiler can instantiate them anywhere they are used.
- Old compilers needed a space in
vector<vector<int> >; modern C++ accepts>>. typenameis required before a dependent type inside a template, e.g.typename T::iterator.
Common mistakes
- Putting a template's definition in a .cpp file and getting linker errors.
- Forgetting
template <>on a full specialization. - Passing a runtime variable where a non-type parameter needs a compile-time constant.
- Omitting
typenamebefore a dependent type.
Write a class template Stack<T, int N> with a fixed capacity using a non-type parameter. Add push and pop, and specialize the print for bool to show true/false.
Summary
- Specialization gives one type a custom template version with
template <>. - Non-type parameters pass compile-time values like sizes.
- Template parameters can have default arguments.
- Specify the type in angle brackets when deduction is not enough.
- Define templates in headers; for the concept, see the main templates lesson.
त्वरित सार
Template किसी भी type के लिए एक बार code लिखता है। यह पृष्ठ अधिक उन्नत रूपों के लिए एक संक्षिप्त syntax reference है; concept, function और class templates, और STL इन्हें कैसे इस्तेमाल करती है, इसके लिए मुख्य templates lesson देखें।
Template specialization
किसी एक type को template <> से उसका अपना version दें:
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 नहीं:
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
template <typename T = int>
class Holder {
T value;
};
// Holder<> h; → default int istemaalCall पर explicit type
जब deduction संभव न हो या आप विशिष्ट type चाहें, इसे angle brackets में नाम दें:
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 <> लिखा, उस मामले के लिए custom व्यवहार देता है, जैसे bool को अलग मानना।C++ में non-type template parameter क्या है?
template <typename T, int N> किसी class template को compile time पर array size N तय करने देता है, जैसे std::array करता है।क्या C++ में template parameters के default values हो सकते हैं?
template <typename T = int> की तरह लिखे, तो आप argument छोड़ सकते हैं और default पा सकते हैं। यह standard library में आम है, जहाँ containers अपने allocator और अन्य policy parameters को default करते हैं।C++ में template type स्पष्ट रूप से कैसे बताते हैं?
maxOf<double>(3, 4) या Box<int>। Function templates के लिए यह केवल तब चाहिए जब compiler arguments से type न निकाल सके; class templates के लिए यह आमतौर पर ज़रूरी है।