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.

Frequently Asked Questions

What is template specialization in C++?
Template specialization lets you provide a different implementation of a template for one specific type. The general template handles most types, while a specialization, written as 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++?
A non-type template parameter is a compile-time value, rather than a type, passed to a template — typically an integer. For example 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++?
Yes. Template parameters can have defaults, written like 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++?
You put the type in angle brackets after the name, such as 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++?
Because the compiler needs the full template definition to instantiate it for each type at the point of use, templates are normally defined in headers rather than separate source files. Splitting a template's declaration and definition across translation units often causes linker errors.

त्वरित सार

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 देखें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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