Anatomy of Templates CSCE 121 J. Michael Moore

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Review for Final Exam Chapters 1–20 (Programming and C++ in 75 minutes) Bjarne Stroustrup Ronnie Ward 1Stroustrup/PPP -
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
Functions Dilshad M. Shahid New York
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Your Message Here. Click box to change background colour.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
Technology icons from magazine These are fully editable vector shapes.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Week 14 - Wednesday.  What did we talk about last time?  More C++  new  delete  Differences for structs  OOP.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
ANIMATED SLIDE VECTOR TRAFFIC LIGHTS. ON CLICK ANIMATION.
CSCE Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 18: Vectors, Templates and Exceptions 1.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Scuola Superiore Sant’Anna Advanced Course on C++ IV Giuseppe Lipari.
Templates C++ template
Variadic templates Bjarne Stroustrup Nov 2010.
Compilation and Debugging
Compilation and Debugging
Anatomy of a class Part II
Anatomy of a class Part I
Templates.
Anatomy of a Function Part 2
Const in Classes CSCE 121 J. Michael Moore.
Computer Organization & Compilation Process
Introduction to the Standard Template Library
CSCE 121- Spring 2016 J. Michael Moore
Valentines Clip Art VALENTINES MESSAGE.
These are fully editable vector shapes
Overloading the << operator
IO Overview CSCE 121 J. Michael Moore
VECTOR TRAFFIC LIGHTS ANIMATED SLIDE.
Data Representation Bits
Manipulators CSCE 121 J. Michael Moore
Array & Pointers CSCE 121 J. Michael Moore.
Return by Reference CSCE 121 J. Michael Moore.
Functions Overview CSCE 121 J. Michael Moore
CSCE , 510, 511, 512 Introduction to Program Design and Concepts Dr. J
Anatomy of a Function Part 1
Anatomy of a Function Part 3
How Functions Work Part 1
Copy Constructor CSCE 121 J. Michael Moore.
Standard Template Library Find
Copy Assignment CSCE 121 J. Michael Moore.
Linked List Insert After
Standard Template Library Model
Static in Classes CSCE 121 J. Michael Moore.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
STL: Traversing a Vector
Parasol Lab, Texas A&M University
CSCE 121 – Spring 2016 Professor J. Michael Moore
Guidelines for Writing Functions
Your message here! TV Template Your name.
Laptop Template Your name.
Anatomy of a Function Part 1
Templates C++ template
VECTOR TRAFFIC LIGHTS ANIMATED SLIDE.
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Data Structures & Programming
Presentation transcript:

Anatomy of Templates CSCE 121 J. Michael Moore Including content created by Bjarne Stroustrup and Jennifer Welch.

Function template <typename TypeName1, typename TypeName2> Typename1 genericFunction(const TypeName1& t1, const TypeName2& t2) { Typename1 temp1; Typename2 temp2; // ... } template <typename TypeName> void otherFunction(TypeName& t) {

Class template <typename TypeName> class MyClass { Typename val; public: void setVal(Typename newVal); } template <typename Typename> void MyClass::setVal(const Typename& newVal) { val = newVal;

Class template <typename T> class MyClass { T val; public: void setVal(T newVal); } void MyClass::setVal(const T& newVal) { val = newVal;

Notes Problems: Recommendation: poor error diagnostics (but improving) delayed error messages (often at link time) all templates must be fully defined in each translation unit (so put everything, including function definitions, in header files) Recommendation: use template-based libraries (e.g., vector) don’t try to do anything fancy yourself (yet) use pass by reference and pass by constant reference