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

Slides:



Advertisements
Similar presentations
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Intro to Generic Programming Templates and Vectors.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Templates Zhen Jiang West Chester University
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Copyright 2006, The Ohio State University Introduction to C++ Templates l C++ Function Templates l C++ Class Templates.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
1 Introduction to Object Oriented Programming Chapter 10.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
 2006 Pearson Education, Inc. All rights reserved Templates.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 22 - C++ Templates
Programming with ANSI C ++
How to be generic Lecture 10
C++ Templates.
Chapter 18 Introduction to Custom Templates
Templates.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Templates in C++.
CMSC 202 Templates.
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CSC 143 Stacks [Chapter 6].
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Parasol Lab, Texas A&M University
Templates I CMSC 202.
Template.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CMSC 202 Lesson 6 Functions II.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Function Templates Class Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
Presentation transcript:

C++ Templates 1

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: 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 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 (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 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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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