C++ Templates.

Slides:



Advertisements
Similar presentations
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
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.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
 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.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Motivation for Generic Programming in C++
Programming with ANSI C ++
Templates.
Exceptions, Templates, and the Standard Template Library (STL)
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Chapter 14 Templates C++ How to Program, 8/e
Chapter 5 Function Basics
Pointers and Pointer-Based Strings
Generic programming – Function template
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS212: Object Oriented Analysis and Design
Starting Out with C++ Early Objects Eighth Edition
Templates.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Lecture 4-7 Classes and Objects
ADT Implementations: Templates and Standard Containers
Templates and Standard Containers
Separate Compilation and Namespaces
Chapter 5 Function Basics
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Name: Rubaisha Rajpoot
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Andy Wang Data Structures, Algorithms, and Generic Programming
Abstraction: Generic Programming, pt. 2
Classes and Objects.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
7 Arrays.
Pointers and Pointer-Based Strings
Lab4 problems More about templates Some STL
CMSC 202 Lesson 6 Functions II.
CPS120: Introduction to Computer Science
SPL – PS1 Introduction to C++.
Presentation transcript:

C++ Templates

Objectives Discuss how reusability and genericity themes have evolved in programming languages Introduce function templates Introduce class templates

Evolution of Reusability, Genericity Major theme in development of programming languages Reuse code Avoid repeatedly reinventing the wheel Trend contributing to this Use of generic code Can be used with different types of data

Evolution of Reusability, Genericity Evolution of algorithmic features of programming languages Evolution of data features of programming languages Niklaus Wirth (inventor of Pascal) Stressed data and algorithms cannot be separated

Function Genericity Overloading and Templates Initially code was reusable by encapsulating it within functions Example lines of code to swap values stored in two variables Instead of rewriting those 3 lines Place in a function void swap (int & first, int & second) { int temp = first; first = second; second = temp; } Then call swap(x,y);

Function Genericity Overloading and Templates To swap variables of different types, write another function Overloading allows functions to have same name Signature (types and numbers of parameters) keep them unique to the compiler This could lead to a library of swap functions One function for each standard type Compiler chooses which to use from signature But … what about swapping user defined types?

Function Templates Note how similar each of the swap functions would be The three places where the type is specified What if we passed the type somehow?!! Templates make this possible Declare functions that receive both data and types via parameters Thus code becomes more generic Easier to reuse

Template Mechanism Declare a type parameter also called a type placeholder Use it in the function instead of a specific type. This requires a different kind of parameter list: ________ void Swap(______ & first, ______ & second) { ________ temp = first; first = second; second = temp; }

Template Mechanism The word template is a C++ keyword Specifies that what follows is … a pattern for a function not a function definition. “Normal” parameters (and arguments) appear within function parentheses Type parameters (and arguments for class templates) appear within template angle brackets (< > ).

Template Mechanism A function template cannot be split across files specification and implementation must be in the same file A function template is a pattern describes how specific function is constructed constructed based on given actual types type parameter said to be "bound" to the actual type passed to it

Template Mechanism Each of the type parameters must appear at least once in parameter list of the function compiler uses only types of the arguments in the call thus determines what types to bind to the type parameters

Function Template template <typename ElementType> void Swap (ElementType &first, ElementType &second) { ElementType temp = first; first = second; second = temp; }

Function Template template <typename ElementType> void Swap (ElementType &first, ElementType &second) Originally, the keyword class was used instead of typename in a type-parameter list. "class" as a synonym for "kind" or "category"— specifies "type/kind" of types.

Function Template <typename ElementType> names ElementType as a type parameter The type will be determined … by the compiler from the type of the arguments passed when Swap() is called.

General Form of Template template <typename TypeParam> FunctionDefinition or template <class TypeParam> FunctionDefinition where: TypeParam is a type-parameter (placeholder) naming the "generic" type of value(s) on which the function operates FunctionDefinition is the definition of the function, using type TypeParam.

Template Instantiation In and of itself, the template does nothing. When the compiler encounters a template it stores the template but doesn't generate any machine instructions. Later, when it encounters a call to Swap() Example: Swap(int1, int2); it generates an integer instance of Swap()

Function Templates When a function template is instantiated (called) Compiler finds type parameters in list of function template For each type parameter, type of corresponding argument determined These two types bound together

Example: Displaying an Array with Template #include <iostream> using namespace std; const unsigned int SIZE = 25; template <typename ElementType> ostream & operator<<(ostream &out, const ElementType list[]); int main() { float array[SIZE]; for(unsigned int i = 0; i < SIZE; i++) cin >> array[i]; cout << array << endl; } ostream& operator<<(ostream &out, const ElementType list[]) for(unsigned int i = 1; i <= SIZE; i++) out << list[i-1] << '\t'; if (i % 10 == 0) out << endl; return out;}

Other Template Examples template <typename T> T max(const T &x, const T &y) { return (x > y) ? x : y; } int main() int i = max(3, 7); // returns 7 double d = max(6.34, 18.523); // returns 18.523 char ch = max('a', '6'); // returns 'a’

Cont. template <typename T> T add(const T &a, const T &b) { return a + b; } int main() int i = add(3, 7); // returns 10 double d = add(6.3, 18.523); // returns 24.823

Class Templates The same idea of templates can be applied to classes. This makes most sense when we have a data structure that can hold multiple values such as an array, list, queue, etc. and the type stored in the data structure can change with the application. Examples might be a list of ints, a list of Text objects, a list of movies, and so on. The following example is a simple template class used for discussion only.

C++ class templates template<class T> class Item { public: Item() ; void SetData(T nValue); T GetData() const; friend ostream & operator<<(ostream &out, const Item<T> & it); private: T Data; }; Item<T> :: Item() : Data( T() ) {} void Item<T> :: SetData(T nValue) Data = nValue } T Item<T> :: GetData() const return Data; ostream & operator<<(ostream &out, const Item<T> & it) { out << it.Data; return out;

Using the Item class Item<int> item1; item1.SetData(120); cout << item1; Item<float> item2; float n = item2.GetData(); item1 = item2; // ERROR WHY?

Rules For Class Templates Definitions of member functions outside class declaration must be function templates. All uses of class name as a type must be parameterized. Member functions must be defined in the same file as the class declaration.

Applying the Rules to Our Item Class Recall how we specified the prototypes in the class Used “T” Thus no changes needed – all rules OK Apply Rule 1 Each member function definition preceded by template<class T>

Applying the Rules to Our Item Class Apply Rule 2 The class name Item preceding the scope operator (::) is used as the name of a type must therefore be parameterized. template<class T> T Item<T> :: GetData() const { … } Apply Rule 3 : specification, implementation in same file

Applying the Rules to Friend Functions Consider the addition of a friend function operator<< Second parameter is of type Item, must beparameterized friend ostream & operator<<(ostream & out, const Item<T>& it);

Applying the Rules to Friend Functions When defining the operator<< function It must be defined as a function template template<class T> ostream & operator<<(ostream &out, const Item<T> & it) { out << it.Data; return out; }

Notes Templates may have more than one type parameter May also have ordinary value parameters

STL (Standard Template Library) A library of class and function templates Components: Containers: Generic "off-the-shelf" class templates for storing collections of data Algorithms: Generic "off-the-shelf" function templates for operating on containers Iterators: Generalized "smart" pointers that allow algorithms to operate on almost any container

Standard Template Library Example of a specific container class iterator algorithm

STL's 10 Containers Kind of Container STL Containers Sequential: deque, list, vector Associative: map, multimap, multiset, set Adapters: priority_queue, queue, stack Non-STL: bitset, valarray, string Much more later.