Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 73

Templates

Written and reviewed by · 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 <>:

C++
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:

C++
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() == 5

Default template arguments

C++
template <typename T = int>
class Holder {
    T value;
};
// Holder<> h;   → uses int by default

Explicit type on a call

When deduction is not possible or you want a specific type, name it in angle brackets:

C++
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 >>.
  • typename is 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 typename before a dependent type.
🏋️ Practice

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 दें:

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() == 5

Default template arguments

C++
template <typename T = int>
class Holder {
    T value;
};
// Holder<> h;   → default int istemaal

Call पर 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 पैदा करता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।