A template defines a family of functions and classes. A template by itself is not a type, or a function, or
any other entity. No code is generated from a source file that contains only template definitions.
In order for any code to appear, a template must be instantiated: the template arguments must be determined so
that the compiler can generate an actual function (or class, from a class template).
Templates allow us to reuse the logic of algorithms for different data types. Without templates, our alternative
for generic algorithms would be type erasing types.
// The "old" way: max operating on nullified pointersvoid*max(void*n1,void*n2,bool(*comp)(void*,void*)){// `comp` is responsible to internally convert `void*` to the appropriate// data type and perform the comparison.return(comp(n1,n2))?n1:n2;}
12
// The "old" way: an extra function operating on the nullified pointersboolcomp(void*a,void*b){return(*(int*)a<*(int*)b);}
123456
// In old C++ and C, we need nullify pointers to create generic functions.// It's easy to see why this is inconvenient.inta=3;intb=9;void*m=max(&a,&b,&comp);std::cout<<"m: "<<*(int*)m<<'\n';