1 C TEMPLATES Function Templates Class Templates 2

  • Slides: 16
Download presentation
1 C++ TEMPLATES Function Templates Class Templates

1 C++ TEMPLATES Function Templates Class Templates

2 Templates overview • Templates define families of classes and functions. • C++ templates

2 Templates overview • Templates define families of classes and functions. • C++ templates enable generic programming. C++ supports both function and class templates. Templates may be parameterized by types, compile-time constants, and other templates. • C++ templates are implemented by instantiation at compile-time.

3 Consider this situation Suppose you wanted to define 2 functions called min that

3 Consider this situation Suppose you wanted to define 2 functions called min that returns the minimum of 2 arguments passed into it for both integers and doubles. You can provide both functions as: int min( int a, int b) { return a < b ? a : b } double min( double a, double b) { return a < b ? a : b } …and so on if you wanted to do this for more types

4 Using of a “function” Template The above (plus a lot more) could also

4 Using of a “function” Template The above (plus a lot more) could also be accomplished using a template function like: template <class T> T min( T a, T b ) { return a < b ? a : b; } The symbol T is called a type parameter. It is simply a place holder that is replaced by an actual type or class when the function is invoked. Note: This function is now defined for all type where “<“ makes sense, not just the intended int and double.

5 Template header • The symbol T is called a type parameter. It is

5 Template header • The symbol T is called a type parameter. It is simply a place holder that is replaced by an actual type or class when the function is invoked. • A function template is declared in the same way as an ordinary function, except that it is preceded by the specification template <class T> • The type parameter T may be used in place of ordinary types within the function definition. • The word class is used to mean a class or primitive type. • A template may have several type parameters, specified like this: template <class T, class U, class V>

6 Calling a templated function Templated functions are called the same way ordinary functions

6 Calling a templated function Templated functions are called the same way ordinary functions are called. int m = 22; int n = 66; int min_value; min_value = min(m, n);

7 Interesting question Happens if we do this: long m = 2; // an

7 Interesting question Happens if we do this: long m = 2; // an int but with twice the bits double n = 1. 8; // a float but with twice the bits. float min_value; min_value = min(m, n); Would this compile? What is min_value? Which templated function ran? int, long or float version

8 Another Interesting question Suppose you created a class ratio which has a protected/private

8 Another Interesting question Suppose you created a class ratio which has a protected/private int num and int den field ratio r(2, 3); // a ratio initialized to 2/3 float n = 0. 8; int min_value = min(r, n); Would this compile? What is min_value? Why?

9 Bubble. Sort anything in C++ template <class T> void swap(T& x, T& y)

9 Bubble. Sort anything in C++ template <class T> void swap(T& x, T& y) { T temp = x; x = y; y = temp; } template<class T> void sort(T* v, int n) { for (int i = 1; i < n; i++) for (int j = 0; j=0, j < n-i; j++) if (v[j] > v[j+1]) swap(v[j], v[j+1] ); }

10 Class Templates Class templates work the same way as a function template except

10 Class Templates Class templates work the same way as a function template except that it generates classes instead of functions. The general syntax is template<class T, …> class X { … } As with function templates, a class template may have several template parameters. Moreover, some of them can be primitive types parameters: template<class T, int n, class U> class X { … }

11 Primitive types must be constant Of course, since templates are instantiated at compile

11 Primitive types must be constant Of course, since templates are instantiated at compile time, values passed to primitive types must be constant: template<class T, int n> class X {}; int main() { X<float, 22> x 1; const int n = 44; X<char, n> x 2; int m = 66; X<short, m> x 3 } // OK //ERROR: m must be constant Note: Class templates are sometimes called parameterized types.

12 Member functions of Class Templates The member functions of a class template are

12 Member functions of Class Templates The member functions of a class template are themselves function templates with the same template header as their class. For example: template<class T> class math. Functions { static T square(T t) {return t*t ; } // more static math functions }; is handled in the same way that the following template function would be handled: template<class T> T square(T t) {return t*t ; }

13 Compiler generated class names It is instantiated by the compiler, replacing the template

13 Compiler generated class names It is instantiated by the compiler, replacing the template parameter T with the type passed to it. Thus, the declaration; math. Functions<short> x; Generated the class and object class math. Functions_short { static short square(short t) { return t*t; } } math. Functions_short x; Note: your compiler may use some name other than math. Functions_short for the class.

14 Example) Stack class template<class T> class Stack { public: Stack (int s =

14 Example) Stack class template<class T> class Stack { public: Stack (int s = 100) : size(s), top(-1), {data = new T[size] ; } ~Stack() {delete [] data; } void push(const T& x) { data[++top] = x; } T pop() {return data[top--]; } int is. Empty() const { return top == -1; } int is. Full() const {return top == size – 1; } private: int size; int top; T* data; }

15 Use of the Stack Template class int main() { Stack<int> int. Stack 1(5)

15 Use of the Stack Template class int main() { Stack<int> int. Stack 1(5) ; Stack<int> int. Stack 2(10); Stack<char> char. Stack(8); int. Stack 1. push(11) ; int. Stack 2. push(22); char. Stack. push(‘A’); int. Stack 2. push(int. Stack 1. pop()); cout << int. Stack 2. pop() << endl; if (int. Stack 2. is. Empty() ) cout << “int. Stack 2 is empty. n” ; } Output: 11 22 int. Stack 2 is empty.

16 Java (mid 90’s) change to C++ (mid 80’s) • Java calls Templates =

16 Java (mid 90’s) change to C++ (mid 80’s) • Java calls Templates = Generics • Java doesn’t use the Template header line template<class T> // C++ template declaration class Stack {…} // Templated Stack definited In Java it becomes: class Stack<T> {…} // Java generic Stack