General Form of Template template<typename TypeParam> Functiondefinition TypeParam is a type-parameter (placeholder) naming the generic type of value(s)on which the function operates FunctionDefinition is the definition of the function, using type TypeParam COMP 152
General Form of Template template<typename TypeParam> FunctionDefinition ▪ TypeParam is a type-parameter (placeholder) naming the "generic" type of value(s) on which the function operates ▪ FunctionDefinition is the definition of the function, using type TypeParam COMP152 11
template<typename T void swap (T& first, T& second) T temp first first second; second tempi (){ int a, bi double x,yi char m ni swap(a, b)i// swap<int>(a, b) swap(x, y)i// swap<double>(x, y) swap(m, n)i// swap<char>(m, n)i COMP 152
COMP152 12 template<typename T> void swap (T& first, T& second) { T temp = first; first = second; second = temp; } main() { int a,b; double x,y; char m,n; swap(a,b); // swap<int>(a,b); swap(x,y); // swap<double>(x,y); swap(m,n); // swap<char>(m,n); }
Template Instantiation In and of itself, the template does nothing When the compiler encounters a template it stores the template but doesn't generate any machine instructions or codes When a function template is instantiated Compiler finds type parameters in list of function template For each type in the function parameter list, type of corresponding argument is determined These two function type and argument type are then bound together E.g., when it encounters a call to swap( EXample: swap(int, int)i it generates an integer instance of swap() The type will be determined by the compiler (at compilation time from the type of the arguments passed when swap() is called Cannot specify data type at run time COMP 152
Template Instantiation ▪ In and of itself, the template does nothing ▪ When the compiler encounters a template • it stores the template • but doesn't generate any machine instructions or codes ▪ When a function template is instantiated • Compiler finds type parameters in list of function template • For each type in the function parameter list, type of corresponding argument is determined • These two function type and argument type are then bound together ▪ E.g., when it encounters a call to swap() • Example: swap(int, int); • it generates an integer instance of swap() ▪ The type will be determined … • by the compiler (at compilation time) • from the type of the arguments passed • when swap() is called ▪ Cannot specify data type at run time COMP152 13
Example: displayarray cpp template <typename T> void display (t array [l, int num)i for (int i =0; i< num; 1++ cout array[i] < i cout < endl oub1ex[]={1.1,2 display <double> created display(x, 5)i int num[]={1,2,3,4}; display(num, 4)i display<int> created 12.23.34.45 1234 Function-template specializations are generated automatically by the compiler to handle each type of call to the function template If an array of user-defined objects is used, need to overload < operator of the object class COMP 152
Example: displayarray.cpp ▪ Function-template specializations are generated automatically by the compiler to handle each type of call to the function template ▪ If an array of user-defined objects is used, need to overload << operator of the object class. COMP152 14 template <typename T> void display(T array[], int num) { for (int i = 0; i < num; i++) cout << array[i] << " "; cout << endl; } int main() { double x[] = {1.1, 2.2, 3.3, 4.4, 5.5}; display(x, 5); int num[] = {1, 2, 3, 4}; display(num, 4); } 1.1 2.2 3.3 4.4 5.5 1 2 3 4 display<double> created display<int> created
h and cpp Files for template Functions a function template cannot be split across files for separate compilation Specification/declaration and implementation/definition usually are in the same file This sometimes causes some inconvenience in makefiles (need to combine h and. cpp Three files foo. h(template declaration foo. cpp(template definition), and (using template functions) The compiler, in the compilation of fc p to object codes, has to know the data type input into the template functions, and replace all the template occurrences by the actual data Therefore, the callers of the template functions have to be known at compile time. This is different from the non- template functions where the compiler does not need to know the callers to generate proper object codes That means main cpp has to include both foo. cpp, and hence foo.h That also means foo. h and foo. cpp have to be combined into one single file COMP 152
.h and .cpp Files for Template Functions ▪ Three files: • foo.h (template declaration), • foo.cpp (template definition), and • main.cpp (using template functions) ▪ The compiler, in the compilation of foo.cpp to object codes, has to know the data type input into the template functions, and replace all the template occurrences by the actual data type • Therefore, the callers of the template functions have to be known at compile time. This is different from the nontemplate functions where the compiler does not need to know the callers to generate proper object codes. • That means main.cpp has to include both foo.cpp,and hence foo.h • That also means foo.h and foo.cpp have to be combined into one single file COMP152 15 A function template cannot be split across files for separate compilation - Specification/declaration and implementation/definition usually are in the same file - This sometimes causes some inconvenience in makefiles (need to combine .h and .cpp)