1 / 15

Templates

Templates. L. Grewe. 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

leora
Download Presentation

Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Templates L. Grewe

  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. Goals • Suppose want the max of two numbers • What kind of numbers? • ints • chars • floats • doubles • All! • How?

  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; }

  5. max function int max(int a, int b) { return a > b ? a : b; } double max( … • 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!

  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');

  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. 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)

  9. Templates template <class T> result ftn(param-list) {…} • 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

  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<class T> T max(T a, T b) { return a > b ? a : b; } x = max(2.5,3.5);

  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; }

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

  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; }

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

  15. What to know about templates • Template Function • a template prefix before the function implementation • template <class Item1, class Item2, ...> • 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\

More Related