Generic Programming Templates and Overloading
Generic Programming: Templates and Overloading
Evolution of reusability and G emeril Major theme in development of programming languages Reuse code to avoid repeatedly reinventing the wheel Trend contributing to this Use of generic code Can be used with different types of data Function and class templates Enable programmers to specify an entire range of related functions and related classes Generic programming
Evolution of Reusability and Genericity ▪ Major theme in development of programming languages • Reuse code to avoid repeatedly reinventing the wheel ▪ Trend contributing to this • Use of generic code • Can be used with different types of data ▪ Function and class templates • Enable programmers to specify an entire range of related functions and related classes • → Generic programming COMP152 2
Function Templates (parameterized functions)
Function Templates (parameterized functions)
Motivation Initially code was reusable by encapsulating it within functions Example Write void swap (int& firstr int& second) int temp= firsti first second second =temp; Then call swap(x,y); COMP 152
Motivation ▪ Initially code was reusable by encapsulating it within functions ▪ Example: • Write • Then call swap(x,y); COMP152 4 void swap (int& first, int& second) { int temp = first; first = second; second = temp; }
To swap variables of different types, write another function Overloading allows functions to have same name Signature(types and numbers of parameters) keep them unique to the compiler This could lead to a library of swap functions One function for each standard/primitive type Compiler chooses which to use from signature But . what about swapping user defined types such as an object? We cannot cover the swap function for ALL possible class objects void swap (int& first, int& second) int temp firsti first second void swap (double& first, double& second) secon d temp double temp firsti first second void swap (char& first, char& second) second temp; first second second temp; COMP 152
▪ To swap variables of different types, write another function • Overloading allows functions to have same name • Signature (types and numbers of parameters) keep them unique to the compiler ▪ This could lead to a library of swap functions • One function for each standard/primitive type • Compiler chooses which to use from signature ▪ But … what about swapping user defined types such as an object? • We cannot cover the swap function for ALL possible class objects COMP152 5 void swap (int& first, int& second) { int temp = first; first = second; second = temp; } void swap (double& first, double& second) { double temp = first; first = second; second = temp; } void swap (char& first, char& second) { char temp = first; first = second; second = temp; }
Passing Types (Instead of Fixed Types void swap (T& first, T& second) T temp- first first second second temp i COMP 152
COMP152 6 void swap (T& first, T& second) { T temp = first; first = second; second = temp; } Passing Types (Instead of Fixed Types)
Using function overloading, note how similar each of the swap functions would be The three places where the type is specified What if we passed the type somehow? Templates make this possible Declare functions that receive both data and types via parameter Thus code becomes more generic Easier to reuse and extend to other types COMP 152
▪ Using function overloading, note how similar each of the swap functions would be • The three places where the type is specified ▪ What if we passed the type somehow? ▪ Templates make this possible • Declare functions that receive both data and types via parameter ▪ Thus code becomes more generic • Easier to reuse and extend to other types COMP152 7
Function Templates Produce overloaded functions that perform identical operations/algorithms on different types of data Programmer writes a single function-template definition Compiler generates separate object-code functions(function template specializations) based on argument types in calls to the function template COMP 152
Function Templates Produce overloaded functions that perform identical operations/algorithms on different types of data • Programmer writes a single function-template definition • Compiler generates separate object-code functions (functiontemplate specializations) based on argument types in calls to the function template COMP152 8
Function Templates More compact and convenient form of overloading Identical program logic and operations algorithms for each data type Function template definition Written by programmer once Essentially defines a whole family of overloaded functions Begins with the template keyword Contains template parameter list of formal type parameters for the function template enclosed in angle brackets(>) Formal type parameters a Preceded by keyword typename or keyword class u Placeholders for fundamental types or user-defined types COMP 152
Function Templates ▪ More compact and convenient form of overloading • Identical program logic and operations/algorithms for each data type ▪ Function template definition • Written by programmer once • Essentially defines a whole family of overloaded functions • Begins with the template keyword • Contains template parameter list of formal type parameters for the function template enclosed in angle brackets (<>) • Formal type parameters ❑Preceded by keyword typename or keyword class ❑Placeholders for fundamental types or user-defined types COMP152 9
Writing Template A function template is a pattern constructed based on given actual types type parameter said to be"bound"to the actual type passed to it Calling a function with template type inside the function template void fo i T f(); COMP 152
Writing Template ▪ A function template is a pattern • constructed based on given actual types • type parameter said to be "bound" to the actual type passed to it ▪ Calling a function with template type inside the function COMP152 10 template void f() { T a; … } f();