Scuola Superiore Sant’Anna Advanced Course on C++ IV Giuseppe Lipari.

Slides:



Advertisements
Similar presentations
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Advertisements

Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Kernighan/Ritchie: Kelley/Pohl:
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
. Plab – Tirgul 10 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
Plab – Tirgul 8 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Defining New Types Lecture 21 Hartmut Kaiser
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
C ++ MULTIPLE CHOICE QUESTION
Chapter 16 Exception Handling
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to Programming
How to be generic Lecture 10
Templates.
Handling Exceptionally Sticky Problems
Exceptions, Templates, and the Standard Template Library (STL)
Why exception handling in C++?
School of EECS, Peking University
Pointers and Pointer-Based Strings
CS212: Object Oriented Analysis and Design
Starting Out with C++ Early Objects Eighth Edition
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Chapter 14: Exception Handling
CMSC 202 Lesson 22 Templates I.
Throwing and catching exceptions
Andy Wang Object Oriented Programming in C++ COP 3330
Exception Handling.
Exceptions 1 CMSC 202.
CISC/CMPE320 - Prof. McLeod
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Pointers and Pointer-Based Strings
Templates I CMSC 202.
Lab4 problems More about templates Some STL
Handling Exceptionally Sticky Problems
Object-Oriented Programming (OOP) Lecture No. 44
Miscellaneous Topics I
CMSC 202 Lesson 20 Exceptions 1.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Scuola Superiore Sant’Anna Advanced Course on C++ IV Giuseppe Lipari

2 Templates Your quote here

3 Templates Templates are the mean for providing generic programming Often, programmers keep doing the same things with slight modifications The most common example is containers –Implementing a list of integers or doubles is not so different –A find() algorithm for a container does not depend on the type of the object

4 Example Suppose we have to swap two integers int a = 2; int b = 4; int tmp = a; a = b; b = tmp; What if we want to swap two generic objects? void swap (int &a, int &b) { int tmp = a; a = b; b = tmp; } template void swap (T &a, T &b) { T tmp = a; a = b; b = tmp; } MyClass a; MyClass b; swap (a,b);

5 Templates Implementing generic containers is the most common use of templates –examples are in the std library, like vector, list, map, etc –see template-stack/ Only when a template is instantiated, the code is generated. –If we do not use swap, the code is never generated, even if we include it! –if there is some error in swap, the compiler will never find it until it tries to generate the code

6 Template instantiation The code for a template should be written all in the header file –it works exactly like inline: the code for the class is generated only when it is used According to Stroustrup (and to the standard), there is a way to write the implementation in the cpp file –you have to use the export keyword –most compilers do not support well this feature

7 Template parameters A template can have any number of parameter A parameter can be –a class, or any predefined type –a function –a constant value (a number, a pointer, etc.) template class Buffer { T v[sz]; int size_; public: Buffer() : size_(i) {} }; Buffer cbuf; Buffer rbuf; void f(int a) { Buffer c; }

8 Default parameters Some parameter can have a default value template sort(It begin, It end);

9 Function template An example of function template is the swap function –other examples are in the STL: sort, find, for_each, etc The compiler tries to automatically deduce the template arguments template void swap (T &a, T &b) { T tmp = a; a = b; b = tmp; } int a = 1; int b = 2; MyClass x,y; swap(a, b); // calls swap swap(x, y); // calls swap

10 Template specialization It is possible to specify the template parameters, all or in part, and provide a specialized implementation –For example, list of pointers is a more specialized case that a generic list. We can provide a special implementation for this special case template class List {…} template<> class List {...} template class List {…}

11 A brief explanation of iterators When you build a container, you need special functions to access the members and traverse the container –One solution could be to write special member functions, like getFirst(), getNext(), and so on –This solution is inflexible, for many reasons internal state for the container you can traverse in one way only generic functions, like sort, could not work in general –A better solution is to provide an additional std class, called iterator, to access the members with a unified interface

12 Iterator An iterator is like a pointer to an object, with restrictions –it can only point to element of a container –operator++() returns the next element in the container –operator*() returns the member itself Every container defines its own iterator class, the interfaces are the same –in this way, it is possible to write generic functions that use only iterators

13 Templates for specifying policies Suppose we want to write a generic sort, like in the STL sort –we only need a way to compare objects, we do not need the type of the object where to put this compare function? –we cannot put it in the container –we cannot put it in the object –we have to provide it separately template

14 Exercise Generalize your list, to contain any kind of object –it must be a template Then, write a Cmp class that compare two strings without considering capital letters –aaa should be equal to AAA Finally, use the my_find function defined in Template_stack/ to look for strings inside your list

15 Exception handling Don’t interrupt me while I’m interrupting - W.S. Churchill

16 Why exception handling The problem of signaling an error… Alternatives –returning an error code –setting a global error variable and returning -1 –exiting We need to write a lot of special purpose code for handling this special cases –we would like, instead, to write less code –see stack/

17 The list implementation You implemented operator[] for random access in the list What if the user specifies an out-of-range index? –we can specify a special “error-return-value” –we can print the error and call exit(); C++ provides an alternative: exceptions

18 Exceptions An exception is an object of a class representing an exceptional occurrence –This way, C++ uses the class mechanisms (like inheritance, etc.) to implement exceptions –The exception class has nothing to do with the other classes in the program An exception can be thrown with the throw keyword see exc_stack/

19 Try/Catch An exception can be caught inside a try/catch block try { // // this code can generate exceptions // } catch (ExcType1&e1) { // all exceptions of ExcType1 } catch (ExcType2 &e2) { // all exceptions of ExcType2 } catch (…) { // every exception }

20 Try/catch If the exception is not caught, then the program is aborted –this is necessary, since the compiler does not know what to do with that exception Usually, it is better to define a hierarchy of exception classes –For example, if you are writing a library for math processing, you can define a base MathExc exception class, and derive any other exception from that class

21 Exception and inheritance In this way, we can throw an object of type LogErr, and catch it as a MathErr class MathExc { string error; string where; public: MathErr(const string &e, const string &w) : error(e), where (w) {} virtual string what() { return error + “ “ + where;} }; class LogErr : public MathErr { public: LogErr() : MathErr(“Log of a negative number”, “log module”), n(a) {} }

22 Exceptions and inheritance This code will print “Log of a negative number - log module” –you can also pass any parameter to LogErr, like the number that cause the error, or the name of the function which caused the error, etc. void f(int i) { mylog(i); } try { // some code f(-5); } catch(MathErr &e) { cout << e.what() << endl; } double mylog(int a) { if (a < = 0) throw LogErr(); else return log(double(a)); }

23 Exception specification It is possible to specify which exceptions a function might throw, by listing them after the function prototype void f(int a) throw(Exc1, Exc2, Exc3); void g(); it means: f() can throw ONLY Exc1, Exc2, Exc3 if you do not put anything, it means that the function g() can throw ANY exception This helps the user of a library however, pay attention at listing all the exception that can be thrown

24 The memory is not deallocated! At this point, a is destructed Exception and memory allocation An exception will “unroll” the stack of a function void f() { A a; if (cond) throw Exc(); } void g() { A *p = new A; if (cond) throw Exc(); }

25 Exception safety There are many problem that pop up from not using exceptions properly –we have seen just one Too many to list here! –if you are interested, please refer to a more specialized book

26 Bibliography “The C++ programming Language” third edition, Bjarne Stroustrup, Addison-Wesley –now there is a special edition “Effective C++, 2nd edition: 50 specific ways to improve your programs and designs” Scott Meyers, Addison-Wesley “Effective STL” Scott Meyers, Addison-Wesley

27 Web sites C/C++ Users Journal – Guru of the week – The Standard template library – Mumit’s STL Newbie guide – And many other!!