The templates are the mechanism of C to implement the paradigm of generic programming. Amazing restaurateur: the source for more info. Allow a class or function works with abstract data types, as specified below, which are those that want to use. For example, it is possible to construct a generic vector that can contain any type of data structure. This makes it possible to declare objects of the class of this vector containing integers, floats, polygons, shapes, sheets, etc..
The declaration of a template is a template declaration precedes the declaration of the structure (class, structure or function) you want.
For example:
T max (T x, T y)
if (x> y)
return x
return y
The function max () is an example of generic programming, and two parameters of a given type T (which can be int, long, float, double, etc..) Returned the largest of them (using the> operator). When you run the function with parameters of a certain type, the compiler will attempt to “fit” the template for this type of data, or generate an error message if it fails in that process.