Templates in C++
One of the greatest strengths of C++ is its support for generic programming. In generic programming, the algorithms and data structures are written in terms of generic types that will be specified later. This allows the programmer to implement a function or a class once, and later, instantiate it for many different types. Templates are a C++ feature that allows classes and functions to be defined on generic types. C++ supports three kinds of templates—function, class, and variable templates.
Function templates
Function templates are generic functions—unlike regular functions, a template function does not declare its argument types. Instead, the types are template parameters:
// Example 01 template <typename T> T increment(T x) { return x + 1; }
This template function can be used to increment a value of any type by one, for which adding one is a valid operation:
increment(5); // T is int, returns 6 increment...