1 / 17

CSCI 383

CSCI 383. Object-Oriented Programming & Design Lecture 23 Martin van Bommel. Templates. A powerful C++ software reuse feature Templates enable programmers to specify, with a single code segment

jackiejones
Download Presentation

CSCI 383

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 383 Object-Oriented Programming & Design Lecture 23 Martin van Bommel

  2. Templates • A powerful C++ software reuse feature • Templates enable programmers to specify, with a single code segment • an entire range of related (overloaded) functions – called function-template specializations, or • an entire range of related classes – called class-template specializations CSCI 383 Lecture 23 M. van Bommel

  3. Function Templates • Overloaded functions normally perform similar or identical operations on different types of data • If the operations are identical for each type, they can be expressed more compactly and conveniently using function templates • Based on the argument types, the compiler generates separate source-code functions to handle each function call appropriately • Called function-template specializations CSCI 383 Lecture 23 M. van Bommel

  4. Function Templates • All function-template definitions begin with keyword template followed by a list of template parameters to the function template enclosed in angle brackets • Each template parameter that represents a type must be preceded by either keyword class or typename template<class T> template<typename T> • Note that keywords class and typename actually mean the same: “any built-in or user-defined type” CSCI 383 Lecture 23 M. van Bommel

  5. Handout #11 • In this example, the compiler creates three function-template specializaitons of printArray • Notice that if T represents a user-defined type in a specialization, there must be an overloading of operator<< for that type CSCI 383 Lecture 23 M. van Bommel

  6. Templates • Although templates offer software-reusability benefits, remember that multiple function and class template specializations are instantiated in a program (at compile time), despite the fact that templates are written only once • These copies can consume considerable memory • But this is not normally an issue because the code generated by the template is the same size as the code you would have written to produce the separate overloaded function CSCI 383 Lecture 23 M. van Bommel

  7. Overloading Function Templates • A function template may be overloaded • Can provide other function templates that specify the same function name but different function parameters • Can provide non-template functions with the same function name but different function arguments (e.g., non-template version of printArray that prints an array of character strings in neat, tabular format) CSCI 383 Lecture 23 M. van Bommel

  8. Overloading Function Templates • Compiler performs matching process to determine what function to call • First, compiler finds all function templates that match function call and creates specializations based on the arguments in the function call • Then, compiler finds all ordinary functions that match the function call • The best match (from ordinary functions and function-template specializations) is chosen • If an ordinary function and a specialization are equally good, the ordinary function is chosen • Otherwise, if multiple matches, ambiguity and error message CSCI 383 Lecture 23 M. van Bommel

  9. Class Templates • It is possible to understand the concept of a “stack” (i.e., data structure with LIFO property) independent of the type of items being placed in the stack • This creates great opportunity for software reusability • We need means for describing notion of a stack generically and instantiating classes that are type-specific versions • C++ provides this capability through class templates CSCI 383 Lecture 23 M. van Bommel

  10. Class Templates • Class templates are called parameterized types because they require one or more type parameters to specify how to customize a “generic class” template • Handout #12 • Note: Most C++ compilers require the complete definition of a template class to appear in the client source-code file that uses the template. For this reason, the implementations of the member functions of class templates are also defined in the header file • Notice the code in function main is almost the same for both the double Stack and the int Stack manipulations • Another opportunity for a function template! • Handout #13 CSCI 383 Lecture 23 M. van Bommel

  11. Non-type Parameters for Templates • It is possible to use non-type template parameters or non-type parameters template<typename T, int elements> • Then, a declaration such asStack<double,100> mostRecentSalesFigure; could be used to instantiate a 100-element Stack class-template specialization of double values. • The class header might contain T stackHolder[elements]; CSCI 383 Lecture 23 M. van Bommel

  12. Default values for Templates • It is possible to have default type arguments in template definitions template<typename T=string> Then, Stack<> jobDescriptions; could be used to create a stack of strings CSCI 383 Lecture 23 M. van Bommel

  13. Templates and Friends • We have seen that functions and entire classes can be declared as friends of non-template classes • With class templates, friendship can be established between • a class template and a global function • a member function of another class (possibly a class-template specialization) • an entire class (possibly a class-template specialization) CSCI 383 Lecture 23 M. van Bommel

  14. Templates and Friends template <class T> class QueueNode { T info; QueueNode *next; ... }; template <class T> class Queue { QueueNode *head, *tail; ... void add_tail(const T& item); T del_head(void); }; CSCI 383 Lecture 23 M. van Bommel

  15. Templates and Friends template <class T> class Queue; // Forward declaration template <class T> class QueueNode { friend Queue<T> T info; QueueNode *next; ... }; CSCI 383 Lecture 23 M. van Bommel

  16. Templates and Static Data Members • Each class-template specialization has its own copy of each static data member of the class template • All objects of that specialization share that one static data member CSCI 383 Lecture 23 M. van Bommel

  17. Templates and Inheritance • Templates and inheritance are related in several ways • A class template can be derived from a class-template specialization • A class template can be derived from a non-template class • A class-template specialization can be derived from a class-template specialization • A non-template class can be derived from a class-template specialization • Have to be careful though CSCI 383 Lecture 23 M. van Bommel

More Related