Labs ICT
๐ŸŽ“ SIWES/IT Login

Templates

1 min read | C++ Tutorial
๐ŸŽ“

Doing SIWES / IT attachment?

Get a structured training programme, a supervised digital logbook, and a completion certificate with LabsICT SIWES / IT.

Explore SIWES / IT Training

C++ Templates

Templates enable generic programming โ€” writing code that works with any data type. Instead of writing separate functions or classes for int, double, string, etc., you write one template and let the compiler generate the appropriate code. This is the foundation of the entire STL.

#include <iostream>

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

int main() {
    std::cout << getMax(10, 20) << "\n";
    std::cout << getMax(3.14, 2.71) << "\n";
    std::cout << getMax('A', 'Z');
    return 0;
}

๐Ÿงช Quick Quiz

What does a template allow you to do?