C Templates 1 Why Use Templates C requires

  • Slides: 23
Download presentation
C++ Templates 1

C++ Templates 1

Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However,

Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example) have almost the same code but with different data types. 2

Why Use Templates? Without templates one of the following options must be used: •

Why Use Templates? Without templates one of the following options must be used: • Re-implement the algorithm with each data type. • Write general code using Object or void* • Using preprocessors 3

Why Use Templates? • Re-implementing causes code duplication and can introduce errors. • Writing

Why Use Templates? • Re-implementing causes code duplication and can introduce errors. • Writing general code bypasses all type-checking. • Preprocessors replace text indiscriminately and can introduce errors by compiling code the programmer never even sees. 4

Example Return the maximum of two parameters. int max(int a, int b) { return

Example Return the maximum of two parameters. int max(int a, int b) { return (a > b) ? a : b; } float max(float a, float b) { return (a > b) ? a : b; } double max(double a, double b) { return (a > b) ? a : b; } void* max(void *a, void *b) { return (*a > *b) ? a : b; } #define MAX(A, B) (a>b)? a: b; 5

Example Instead, this function can be implemented using function templates. template <typename T> T

Example Instead, this function can be implemented using function templates. template <typename T> T max(T a, T b) { return (a > b) ? a : b; } The type used by this function is determined when it is called. int main() { cout << max(5, 6) << endl; } 6

Example For the compiler to match the type with the template, the type of

Example For the compiler to match the type with the template, the type of both arguments must match. The following will cause an error: template <typename T> T max(T a, T b) { return (a > b) ? a : b; } int main() { cout << max(5, 6. 0) << endl; } 7

Example This can be solved in several ways: template <typename T> T max(T a,

Example This can be solved in several ways: template <typename T> T max(T a, T b) { return (a > b) ? a : b; } int main() { cout << max((double)5, 6. 0) << endl; cout << max<double>(5, 6. 0) << endl; } Or: template <typename T 1, typename T 2> T 1 max(T 1 a, T 2 b) { return (a > b) ? a : b; } 8

Example However, this causes a new problem. The return type is defined as the

Example However, this causes a new problem. The return type is defined as the first type. int main() { cout << max(5, 6. 0) << endl; //is different to cout << max(6. 0, 5) << endl; } 9

Overloading Function Templates Like other functions in C++, function templates can be overloaded. For

Overloading Function Templates Like other functions in C++, function templates can be overloaded. For example with a specific type. int max(int a, int b) { return (a > b) ? a : b; } template <typename T> T max(T a, T b) { return (a > b) ? a : b; } int main() { cout << max(5, 6) << endl; //uses non-template cout << max(5. 0, 6. 0) << endl; //uses template } 10

Class Templates The next common use of templates is class templates. This is especially

Class Templates The next common use of templates is class templates. This is especially useful for container classes that are used to store objects. template <typename T> class Stack { private: std: : vector<T> items; public: void push(T const&); void pop(); T top() const; bool empty() const { return items. empty(); } }; 11

Class Templates Members functions need to be written with template information. template <typename T>

Class Templates Members functions need to be written with template information. template <typename T> void Stack<T>: : push(T const &a) { items. push_back(a); } These classes can be used by defining the data type they will be used to store: int main() { Stack<int> int. Stack; Stack<float> float. Stack; Stack<string> string. Stack; } 12

Class Templates Classes can be specialised for particular types. template <> class Stack<int> {

Class Templates Classes can be specialised for particular types. template <> class Stack<int> { private: vector<int> items; public: void push(int); void pop(); int top(); bool empty() const {return items. empty(); } }; 13

Class Templates A class can also be partially specialised. template <typename T 1, typename

Class Templates A class can also be partially specialised. template <typename T 1, typename T 2> class my. Class{. . . }; template <typename T> class my. Class<T, T>{. . . }; template <typename T> class my. Class<T, int>{. . . }; template <typename T 1, typename T 2> class my. Class<T 1*, T 2*>{. . . }; 14

Class Templates Or with default template arguments: template <typename T, typename CONT=vector<T> > class

Class Templates Or with default template arguments: template <typename T, typename CONT=vector<T> > class Stack { private: CONT items; public: void push(T const &); void pop(); T top() const; bool empty() const {items. empty(); } }; int main() { Stack<int> vector. Stack; //uses vector Stack<int, deque<int> > deque. Stack; //uses deque } 15

Nontype Template Parameters Template parameters don’t necessarily have to be types. These parameters can

Nontype Template Parameters Template parameters don’t necessarily have to be types. These parameters can also be ordinary values: template <typename T, int MAXSIZE> class Stack { private: T items[MAXSIZE]; int num. Items; public: Stack(); void push(T const &); void pop(); T top() const; bool empty() const; bool full() const; }; 16

Nontype Template Parameters This allows a Stack to be created with a specific type

Nontype Template Parameters This allows a Stack to be created with a specific type and size. int main() { Stack<int, 20> int 20 Stack; Stack<int, 40> int 40 Stack; } It is important to note that these two instances are of different types. Stack<int, 20> is a different type to Stack<int, 40>. 17

Template Parameters A template parameter can also be a class template. This can be

Template Parameters A template parameter can also be a class template. This can be useful to avoid the following situations: template <typename T, typename CONT=vector<T> > class Stack {. . . }; int main() { Stack<int> vector. Stack; //uses vector Stack<int, deque<int> > deque. Stack; //uses deque } Here the type of the Stack is defined twice. int and deque<int> 18

Template Parameters A template parameter can also be a class template. This can be

Template Parameters A template parameter can also be a class template. This can be useful to avoid the following situations: template <typename T, typename CONT=vector<T> > class Stack {. . . }; int main() { Stack<int> vector. Stack; //uses vector Stack<int, deque<int> > deque. Stack; //uses deque } Here the type of the Stack is defined twice. int and deque<int> 19

Template Parameters It would be better to be able to define the stack as:

Template Parameters It would be better to be able to define the stack as: int main() { Stack<int, vector> vector. Stack; //uses vector Stack<int, deque> deque. Stack; //uses deque } To do this we have to define the second parameter as a template parameter. The following code defines this. template <typename T, template<typename ELEM> class CONT = vector > class Stack {. . . }; 20

Template Parameters However, there is a problem with this. The std vector has more

Template Parameters However, there is a problem with this. The std vector has more than one parameter. The second is the allocator. This must be defined in order to make the templates work: template <typename T, template<typename ELEM, typename ALLOC = allocator<ELEM> > class CONT = vector > class Stack {. . . }; 21

Template Parameters All this templates code just so we can create a Stack using

Template Parameters All this templates code just so we can create a Stack using code like this: int main() { Stack<int, vector> vector. Stack; Stack<int, deque> deque. Stack; } 22

Summary Function Templates Class Templates Parameterised Templates Type Parameters Nontype Parameters Template Parameters 23

Summary Function Templates Class Templates Parameterised Templates Type Parameters Nontype Parameters Template Parameters 23