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
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 2
Motivation Initially code was reusable by encapsulating it within functions Example Write void swap (int& first, int& second) int temp= firsti first second second =temp; Then call swap(x,y);
Motivation ▪ Initially code was reusable by encapsulating it within functions ▪ Example: • Write • Then call swap(x,y); 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 void swap (int& first, int& second) int temp firsti first second: second temp void swap (double& first, double& second double temp first first second void swap (char& first, char& second second temp first second second temp; But . what about swapping user defined types such as an object? We cannot cover the swap function for ALL possible class objects
▪ 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 = 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
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
▪ 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
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 Generate and compile declaration or definition? compiler does more things
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 Generate and compile, declaration or definition? compiler does more things …
General Form of Template template Function Definition with the parameter type T T is a type-parameter(placeholder)naming the"generic"type of value(s)on which the function operates Function Definition is the definition of the function, using type T Can use 'class'or 'typename, so it is a type name, so 'typename'is better than the old'class
General Form of Template template Function Definition with the parameter type T … Can use ‘class’ or ‘typename’, so it is a type name, so ‘typename’ is better than the old ‘class’! ▪ T is a type-parameter (placeholder) naming the "generic" type of value(s) on which the function operates ▪ Function Definition is the definition of the function, using type T