Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js Asp.net Core C C#
      DotNet HTML/CSS Java JavaScript Node.js
      Python React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C++
  4. Complete Guide to Using Templates in C++ with Examples

Complete Guide to Using Templates in C++ with Examples

Date- Dec 09,2023

Updated Feb 2026

3215

c++ c++ templates

Understanding Templates in C++

Templates in C++ provide a mechanism for creating functions and classes that can work with any data type. This feature is crucial for developing software that is both efficient and easy to manage. Templates help reduce code duplication and improve code clarity, as developers can write a single implementation that works across various data types.

Real-world applications of templates include data structures such as linked lists, trees, and stacks, as well as algorithms like sorting and searching. By using templates, developers can create libraries that are more versatile and easier to maintain, ultimately leading to better software design.

Prerequisites

Before diving into templates in C++, it is essential to have a basic understanding of the C++ programming language, including concepts such as variables, data types, functions, and classes. Familiarity with object-oriented programming principles will also be beneficial, as templates are often used in conjunction with classes.

1. Function Templates

Function templates allow you to create generic functions that can operate on different data types. You define a function template by specifying a placeholder for the data type, which can be replaced with any specific data type when the function is called. This makes it easy to write functions that can handle a variety of inputs without duplicating code.

#include <iostream>
using namespace std;
template <class T>
T add(T x, T y) {
    return x + y;
}

int main() {
    cout << "Addition of integers: " << add(5, 10) << endl;
    cout << "Addition of doubles: " << add(5.5, 10.5) << endl;
    return 0;
}

In this example, the function template add can accept both integers and doubles. When you call add(5, 10), the compiler generates a version of the function that works with integers, while add(5.5, 10.5) generates a version that works with doubles.

Complete Guide to Using Templates in C with Examples

2. Class Templates

Class templates allow you to define generic classes that can operate with any data type. This is particularly useful for creating data structures that need to handle various types of data without rewriting the entire class for each data type.

#include <iostream>
using namespace std;
template <class T>
class Stack {
    private:
        T* arr;
        int top;
        int capacity;
    public:
        Stack(int size);
        ~Stack();
        void push(T x);
        T pop();
        bool isEmpty();
};

template <class T>
Stack<T>::Stack(int size) {
    arr = new T[size];
    capacity = size;
    top = -1;
}

template <class T>
Stack<T>::~Stack() {
delete[] arr;
}

template <class T>
void Stack<T>::push(T x) {
    if (top == capacity - 1) {
        cout << "Stack Overflow" << endl;
        return;
    }
    arr[++top] = x;
}

template <class T>
T Stack<T>::pop() {
    if (isEmpty()) {
        cout << "Stack Underflow" << endl;
        return T();
    }
    return arr[top--];
}

template <class T>
bool Stack<T>::isEmpty() {
    return top == -1;
}

int main() {
    Stack<int> intStack(5);
    intStack.push(1);
    intStack.push(2);
    cout << "Popped from intStack: " << intStack.pop() << endl;

    Stack<double> doubleStack(5);
    doubleStack.push(3.14);
    doubleStack.push(2.71);
    cout << "Popped from doubleStack: " << doubleStack.pop() << endl;

    return 0;
}

This example demonstrates a simple stack implementation using a class template. The class Stack can store any data type, and methods like push and pop operate on that type.

Complete Guide to Using Templates in C with Examples 2

3. Template Specialization

Template specialization allows you to define a specific implementation of a template for a particular data type. This is useful when you need a different behavior for a specific type than what is provided by the generic template.

#include <iostream>
using namespace std;
template <class T>
class Printer {
    public:
        void print(T value) {
            cout << "Value: " << value << endl;
        }
};

template >>
class Printer<char> {
    public:
        void print(char value) {
            cout << "Character: " << value << endl;
        }
};

int main() {
    Printer<int> intPrinter;
    intPrinter.print(42);

    Printer<char> charPrinter;
    charPrinter.print('A');

    return 0;
}

In this example, the Printer class is specialized for the char type, providing a different implementation for printing characters. This allows for more tailored behavior while still using the power of templates.

Complete Guide to Using Templates in C with Examples 3

4. Variadic Templates

Variadic templates extend the capabilities of templates by allowing you to accept an arbitrary number of template parameters. This feature is particularly useful for creating functions that can handle a variable number of arguments.

#include <iostream>
using namespace std;
template <typename... Args>
void printAll(Args... args) {
    (cout << ... << args) << endl;
}

int main() {
    printAll(1, 2.5, "Hello", 'A');
    return 0;
}

The printAll function can accept any number of arguments of any type, demonstrating the flexibility of variadic templates. This feature is beneficial for logging functions, where the number of parameters may vary.

Complete Guide to Using Templates in C with Examples 4

Edge Cases & Gotchas

While templates offer significant advantages, there are some edge cases and potential pitfalls to be aware of. For instance, when using templates with pointers or references, you must ensure that the correct type is used to avoid undefined behavior. Additionally, template code can lead to longer compile times, as the compiler generates separate instances of the template for each unique type used.

Another gotcha is that template errors can sometimes be difficult to decipher, as they often manifest as long compiler error messages. It's essential to read these messages carefully and understand the root cause of the issue.

Performance & Best Practices

When using templates in C++, there are several best practices to keep in mind:

  • Use meaningful names: Template parameters should have descriptive names, such as T or Type, to make the code easier to understand.
  • Limit specialization: While template specialization can be powerful, overusing it can lead to code that is hard to maintain. Use it only when necessary.
  • Prefer auto when possible: In many situations, using auto can help simplify template code and improve readability.
  • Document your code: Since templates can be complex, adding comments and documentation can help other developers understand your design choices.

Conclusion

Templates in C++ are a powerful tool for creating generic and reusable code. They enable developers to write functions and classes that can work with any data type while reducing code duplication and improving maintainability.

  • Templates enhance code flexibility and reusability.
  • Function templates and class templates are the two main types.
  • Template specialization and variadic templates provide advanced features.
  • Be aware of edge cases and best practices to avoid common pitfalls.
Complete Guide to Using Templates in C with Examples 5

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Essential Java Tools: A Complete Guide to Chapter 2 Fundamentals
Dec 09, 2023
Understanding Variables in Python: A Complete Guide with Examples
Dec 09, 2023
Parameterised constructor in java
Sep 09, 2023
Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
Previous in C++
Mastering Functions in C++: A Complete Guide with Examples

Comments

On this page

More in C++

  • Complete Guide to C++ Classes: Explained with Examples 3750 views
  • Mastering Inheritance in C++: A Complete Guide with Examples 3490 views
  • Input/Output Statements in C++ 3439 views
  • Mastering Functions in C++: A Complete Guide with Examples 3437 views
  • Introduction in C++ 3129 views
View all C++ posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor