Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)

Similar presentations


Presentation on theme: "C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)"— Presentation transcript:

1 C++ Templates 1

2 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

3 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

4 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

5 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

6 Example Instead, this function can be implemented using function templates. template 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

7 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 T max(T a, T b) { return (a > b) ? a : b; } int main() { cout << max(5,6.0) << endl; } 7

8 Example This can be solved in several ways: template T max(T a, T b) { return (a > b) ? a : b; } int main() { cout << max((double)5,6.0) << endl; cout (5,6.0) << endl; } Or: template T1 max(T1 a, T2 b) { return (a > b) ? a : b; } 8

9 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

10 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 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

11 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 class Stack { private: std::vector items; public: void push(T const&); void pop(); T top() const; bool empty() const { return items.empty();} }; 11

12 Class Templates Members functions need to be written with template information. template void Stack ::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 intStack; Stack floatStack; Stack stringStack; } 12

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

14 Class Templates A class can also be partially specialised. template class myClass{... }; template class myClass {... }; template class myClass {... }; template class myClass {... }; 14

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

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

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

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

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

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

21 Template 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 > class CONT = vector > class Stack {... }; 21

22 Template Template Parameters All this templates code just so we can create a Stack using code like this: int main() { Stack vectorStack; Stack dequeStack; } 22

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


Download ppt "C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)"

Similar presentations


Ads by Google