Why we need Templates?
Suppose we are writing a function to compare the values of two variables and return the one with greater value, the function would look like this (for an int):
what if we would like to support
hmmm, works like a charm. But you have to write definitions (overload) if you want more types to be supported. It gets ugly very quickly with moderately complex logic. So wouldn’t it be nice to have some language feature that could do the work of replication of code for us? Well it happens that C++ supports such feature, infamously known as Templates.
Consider these three definitions of
if you observe closely you will notice that all three definitions look similar. So if we try to abstract the structure such that the logic remains same but the types vary then we should be able to write something like this:
Lets dissect the above code:
This statement defines the Template Function, in our case it is function but it can be a class as well, that is being parametrized with a single type
T will be replaced by required type, we will get into this a bit later.
Here the function
max takes in two arguments
b and returns the one that is greater of the two. The type of these arguments is
T, as in
template<class T> above.
Ok so we are done with the description and here is the how the compiler generates the code for various types on the fly.
Here compiler see that function
max is being called with two integers, note both the arguments have to be of same type with templates as no implicit type conversion happens like
double, so compiler can successfully deduce that we want the
max function definition for an
int. Compiler creates a new copy of the template code and replaces
This act of creating functions (and classes as well) on the fly from Template Definitions is called Template Instantiation. Template Instantiation is two step process:
- Compiler checks Template Definition for syntatic inconsistencies, in our case the Templated Parameter
Tis being compared (
operator >), here compiler assumes that the type
T, can be user defined type as well, defines
- Once the compiler finds that the Template Definition is syntactically consistent, then in second step it checks for sematic inconsistencies,
operator >is a good example here. If we have our own user defined
class Comparable, then for it to work with Template Function
max, we will have to define
class Comparable. When instantiating Templates compiler creates a copy of the code, hence it needs to see the entire definition. Thus the Templates are usually placed in header files.
Here is the Comparable class:
Thus you can now use Template Function
max with any user defined type, if that type provides
operator > for comparison. We have barely scratched the surface about what can be done with C++ Templates, nonetheless it is good beginning. In coming posts I will cover Template classes and various scenarios. I will briefly cover STL (Standard Template Library) and BOOST library.