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.
Frequently Asked Questions
What is template specialization in C++?
template <> with the concrete type, gives custom behaviour for that case, such as treating bool differently.What is a non-type template parameter in C++?
template <typename T, int N> lets a class template fix an array size N at compile time, as std::array does.Can template parameters have default values in C++?
template <typename T = int>, so you may omit the argument and get the default. This is common in the standard library, where containers default their allocator and other policy parameters.How do you explicitly specify a template type in C++?
maxOf<double>(3, 4) or Box<int>. For function templates this is only needed when the compiler cannot deduce the type from the arguments; for class templates it is usually required.Why must template definitions usually go in header files in C++?
त्वरित सार
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 देखें।