Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.

Similar presentations


Presentation on theme: "Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic."— Presentation transcript:

1 Templates L. Grewe

2 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic thinking –  computer science –  “functionalism” in phil. of mind –  abstraction Sometimes want to do basically the same thing with different types of things –LL of ints, LL of widgets –“abstract data types”

3 3 Goals Suppose want the max of two numbers What kind of numbers? –ints –chars –floats –doubles All! How?

4 4 Max with typedef Suppose want the max of two numbers What kind of numbers? ints chars floats doubles All! How? typedef... dtype; dtype max(dtype a, dtype b) { return a > b ? a : b; } typedef... dtype; dtype max(dtype a, dtype b) { return a > b ? a : b; }

5 5 max function Soln 1: Write one function for each type, e.g.: Is allowed in C++ (“overloading”) But this is manually duplicating code –for nontrivial ftns – very bad –hard to maintain! int max(int a, int b) { return a > b ? a : b; } double max( … int max(int a, int b) { return a > b ? a : b; } double max( …

6 6 max functions Soln 2: Write one, maxly general function This works but it’s not nice –All four types can widen to doubles –but must be cast back double max(double a, double b) { return a > b ? a : b; } double x = max(2.5, 3.5); char c = (char)max('A','B'); double max(double a, double b) { return a > b ? a : b; } double x = max(2.5, 3.5); char c = (char)max('A','B');

7 7 max functions Soln 3: Use the C++ preprocessor macros C++ source code is preprocessed –#includes replaced with header files –#ifndef, etc. –macro calls replaced with macro content  Works too, but complications, e.g.: –  –x, y inc-ed twice Need many parentheses – sq(a+b), etc. #define max(a,b) (a > b ? a : b) int c = max(2,3); int c = (2 > 3 ? 2 : 3); z = max(x++, y++) z = (x++ > y++ ? x++ : y++)

8 8 max functions Soln 4: Use the CPP in a more sophisticated way Don’t use the CPP to generate expressions but to generate functions, e.g.: Avoids the previous CPP problems But reqs code for all poss types –Done partly manually –Increases executable size #define define_max(t)\ t max(t a, t b) {\ return a > b ? a : b; \ } define_max(char) define_max(int) #define define_max(t)\ t max(t a, t b) {\ return a > b ? a : b; \ } define_max(char) define_max(int)

9 9 Templates –T is a place-holder for the substituted type –template and class are both keywords T is some type –Primitive, class, array, etc. –All occurrences of T in ftn replaced with the real type used in particular case template result ftn(param-list) {…}

10 10 max functions Soln 5: use templates –parameterized function –expands per type as necessary Now can simply call the function: – Compiler automatically creates only the function specializations needed template T max(T a, T b) { return a > b ? a : b; } template T max(T a, T b) { return a > b ? a : b; } x = max(2.5,3.5);

11 11 Templates: swapping How to swap two ints? Suppose we want to swap other types  templates void swap(int &a, int &b) { int c = a; a = b; b = c; } void swap(int &a, int &b) { int c = a; a = b; b = c; }

12 12 Generic swapping Now can swap any prim Can also swap any objects –As long as = op is public and works correctly! template void swap(T &a, T &b) { T c = a; a = b; b = c; } template void swap(T &a, T &b) { T c = a; a = b; b = c; }

13 13 Template specialization string s,t … max(s,t); works But max("hi","there") doesn’t: if ("hi" < "there") … compares two pointers - where the char[] s start –Not what we mean Solution: create a specialization –special version for this case –We check for spec. before template char *max(char *a, char *b) { return strcmp(a,b) > 0 ? a : b; } char *max(char *a, char *b) { return strcmp(a,b) > 0 ? a : b; }

14 14 Template Classes Not just Templates for functions but, also classes. Define How to use it? Consider a node class defined with templates. node * age = NULL; ages.set(18); node name; name.set("Jorge"); node *grid; grid = new node ; grid->set(point(2,4)); Consider a node class defined with templates. node * age = NULL; ages.set(18); node name; name.set("Jorge"); node *grid; grid = new node ; grid->set(point(2,4)); Template class node { T value; //member functions template T node ::set(T v){ value= v; } } Template class node { T value; //member functions template T node ::set(T v){ value= v; } }

15 15 What to know about templates Template Function –a template prefix before the function implementation –template Function Prototype –a template prefix before the function prototypes Template Class –a template prefix right before the class definition Instantiation –template functions/classes are instantiated when used\


Download ppt "Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic."

Similar presentations


Ads by Google