C++ template Keyword

The template keyword in C++ is used to create generic classes and functions. It allows writing code that works with any data type, making it highly reusable and flexible. Templates enable type-independent programming, where the exact data type is specified at compile-time.

Templates are particularly useful in scenarios where the same logic applies to different data types, such as sorting algorithms, mathematical operations, or container classes.


Syntax

Template Function Syntax

</>
Copy
template
return_type function_name(T parameter) {
    // Function body
}

Template Class Syntax

</>
Copy
template
class ClassName {
    T member;
public:
    void method_name(T parameter);
};
template
Declares the use of a template.
typename T
Specifies a placeholder for a data type, where T can be any valid type.

Examples

Example 1: Template Function

This example demonstrates a template function for finding the maximum of two values.

</>
Copy
#include <iostream>
using namespace std;

template<typename T>
T findMax(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    cout << "Max of 3 and 7: " << findMax(3, 7) << endl;
    cout << "Max of 5.5 and 2.2: " << findMax(5.5, 2.2) << endl;
    return 0;
}

Output:

Max of 3 and 7: 7
Max of 5.5 and 2.2: 5.5

Explanation:

  1. The findMax template function works with any type T specified at compile-time.
  2. In main(), findMax is called with both integer and double arguments, and the compiler generates the appropriate code for each type.

Example 2: Template Class

This example demonstrates a template class for a simple stack.

</>
Copy
#include <iostream>
using namespace std;

template<typename T>
class Stack {
    T arr[100];
    int top;
public:
    Stack() : top(-1) {}

    void push(T value) {
        if (top < 99) {
            arr[++top] = value;
        } else {
            cout << "Stack overflow" << endl;
        }
    }

    T pop() {
        if (top >= 0) {
            return arr[top--];
        } else {
            cout << "Stack underflow" << endl;
            return T();
        }
    }
};

int main() {
    Stack<int> intStack;
    intStack.push(10);
    intStack.push(20);
    cout << "Popped: " << intStack.pop() << endl;

    Stack<string> stringStack;
    stringStack.push("Hello");
    stringStack.push("World");
    cout << "Popped: " << stringStack.pop() << endl;
    return 0;
}

Output:

Popped: 20
Popped: World

Explanation:

  1. The Stack template class is defined to work with any type T.
  2. The push and pop methods operate on elements of type T.
  3. In main(), two stacks are created: one for integers and another for strings.
  4. The compiler generates separate versions of the Stack class for each type.

Key Points to Remember about template Keyword

  1. The template keyword is used to define generic classes and functions.
  2. Templates provide type independence, making code reusable for multiple data types.
  3. The typename or class keyword can be used to specify a template parameter.
  4. The compiler generates specific code for each type used with the template.
  5. Templates are a cornerstone of the C++ Standard Template Library (STL), enabling features like vectors, lists, and maps.