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<typename T> 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<typename T> void f() { T a; … } f();