Templates

2 types:

  • Function templates
  • Class templates

Function templates act like a stencil without having to specify the exact data-types of some / all of the variables. Instead the function is defined using placeholder data-types known as template type parameters.

 

When a function template is called, the compiler stencils in a copy of the template replacing the placeholder data-types with the actual variable data-types in the function call. Thus multiple data-types can be sent as parameters to the function template.

 

When the compiler encounters a template function call, it replicates the template function and replaces the template type parameters with the actual data types; this is known as a function template instance.

 

Here is the general syntax for a function template:

template <typename Type>  //template data-type parameter declaration
ret-type funcName(Type param1, Type param2 ) {
	...func code...
}

*note: need to check, but it seems the function template must follow immediately after the template parameter declaration

 

 

The keyword template informs the compiler that what follows is a list of template parameters, which are placed within <angled brackets>.

 

To create a template parameter the keyword typename or class before your chosen data-type placeholder name.

*(note the keyword class is quite used interchangeably, and makes little difference in most contexts).

**(also note that traditionally an uppercase "T" is used to denote the name "Type" or data-type - however, for simplicity I have chosen to use myType in the following example)

 

This example declares a template parameter to be used as the placeholder, which is then replaced when the function is called, based on the data-type of the parameters being sent to the template function:

#include <iostream>
using namespace std;

template <typename myType> // this is the template parameter declaration
myType funcMax(myType x, myType y) {  //now using myType

	return (x > y) ? x : y;
}

int main() {

	int a = 12, b = 17 ;
	cout << funcMax(a, b) << endl ;

	double c = 36.42, d = 17.93 ;
	cout << funcMax (c, d) << endl ;

	char e = 'j', f = '11' ;
	cout << funcMax(e, f) << endl ;

	return 0;

}

Compile & Run:

17
36.42
j

 

*note: when creating the above, I found out that the standard library already has a templated max() function, and since I have the statement "using namespace std ;" the compiler was unable to tell which version of the max() function I required. I therefore changed mine in the example above to funcMax() ;

 

 

Class templates pretty much follow the same logic as function templates by replacing the templated self defined data-type into the class

 

#include <iostream>
using namespace std ;

template <class myType>
class Maximus {
private:
	myType x, y ;  //here we see the generic myType being used
public:
	Maximus (myType alpha, myType beta){ //again the generic myType
		x = alpha ;
		y = beta ;
	}
	myType getMax() ; //and again within the prototype
};

template <class myType>
myType Maximus<myType>::getMax(){ //the method definition also using the generic myType
	return (x > y) ? x : y;
}

int main() {
	//now an object of the Maximus class data-type is instantiated
	Maximus <int> myObject(42, 17) ;  //using Ints in the definition
	cout << myObject.getMax() << endl ;;

	Maximus <double> newObject(123.456, 789.012) ; //this time using doubles in the definition
	cout << newObject.getMax() << endl ;

	Maximus <char> nextObject('j', '11') ; //this time using chars in the definition
	cout << nextObject.getMax() << endl ;

	return 0;
}

Compile & Run:

42
789.012
j

Leave a Reply