161 likes | 262 Views
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
E N D
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 • Sometimes want to do basically the same thing with different types of things • LL of ints, LL of widgets • “abstract data types”
Goals • Suppose want the max of two numbers • What kind of numbers? • ints • chars • floats • doubles • All! • How?
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; }
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!
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');
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++)
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)
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
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);
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; }
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!
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; }
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));
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\