Download presentation
Presentation is loading. Please wait.
Published byYeter Deniz Modified over 6 years ago
1
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Templates (again)
2
Outline Function templates and related (overloaded) functions
Distinguishing between function templates and function-template specializations Class templates to create a group of related types Distinguishing between class templates and class-template specializations Overloading function templates Relationships among templates, inheritance, friends, and static members CS-2303, A-Term 2012 Templates (again)
3
Review — Function Templates
Written by programmer once Defines a whole family of overloaded functions Begins with the template keyword Contains template parameter list of formal types Formal type parameters Enclosed in angle brackets (<>) Preceded by keyword typename or keyword class Placeholders for fundamental or user-defined types Similar to macros in C, but with full type checking Week 5, Miscellaneous C++ topics §16.1 of Absolute C++ CS-2303, A-Term 2012 Templates (again)
4
Software Engineering Note
Most C++ compilers require complete definition of template in client source-code using the template Templates often defined in header files Included via #include into the appropriate client files For class templates, member functions must also be defined in header file CS-2303, A-Term 2012 Templates (again)
5
Function Template — Example Forms
Common Programming error:– Forgetting to say class or typename template<typename T> f() { } template<class ElementType> g(){ } template<typename BorderType, typename Filltype> h() { } Another common Programming error:– Forgetting to say class or typename for every parameter CS-2303, A-Term 2012 Templates (again)
6
Common Programming Error
If template is invoked with user-defined type, and … … if template uses functions or operators (e.g., ==, +, <=) with objects of class type, then … … those functions and operators must be overloaded for the user-defined type Forgetting to overload such operators causes compilation errors CS-2303, A-Term 2012 Templates (again)
7
Function Template Specialization
Definition:– Function Template Specialization Specialization:– the automatic generation of a new “overloaded function” from the template as needed I.e., whenever a function template is called with a particular type Example for function template max with type parameter T called with int arguments Compiler detects a max invocation in the program code. int is substituted for T throughout the template definition. This produces function-template specialization max<int> CS-2303, A-Term 2012 Templates (again)
8
Overloading Function Templates
Other function templates that specify the same name but different parameters Non-template functions that specify the same name but different parameters The compiler chooses best function or specialization to match the function call A non-template function is chosen over a template specialization in case of a tie Otherwise, multiple matches results in a compilation error (the compiler considers the function call to be an ambiguous function call ) CS-2303, A-Term 2012 Templates (again)
9
Questions about Function Templates?
CS-2303, A-Term 2012 Templates (again)
10
Class Templates Class-template definitions are preceded by a header
Also known as Parameterized Types Class Templates Class-template definitions are preceded by a header Such as template< typename T > Type parameter T can be used as data type in members functions and data Multiple type parameters can be specified using comma-separated list – e.g., template< typename T1, typename T2 > CS-2303, A-Term 2012 Templates (again)
11
Class Templates (continued)
Class templates encourage software reusability … … by enabling type-specific versions of generic classes to be instantiated CS-2303, A-Term 2012 Templates (again)
12
Example Stack.h (1 of 3) Create class template Stack with type parameter T Member functions with type parameter T in parameter lists CS-2303, A-Term 2012 Templates (again)
13
Example (continued) Stack.h (2 of 3)
Data member stackPtr is pointer to T Member-function template definitions appearing outside class-template definition begin with template header CS-2303, A-Term 2012 Templates (again)
14
Note: function bodies are spelled out in class header file!
Stack.h (3 of 3) Note: function bodies are spelled out in class header file! CS-2303, A-Term 2012 Templates (again)
15
T might have to overload operator =
Stack.h (3ʹ of 3) T might have to overload operator = Why? Digression: What if T is an abstract class with subclasses? CS-2303, A-Term 2012 Templates (again)
16
Example.cpp (1 of 2) Create specialization Stack< double >
CS-2303, A-Term 2012 Templates (again)
17
Example.cpp (2 of 2) Create specialization Stack< int >
CS-2303, A-Term 2012 Templates (again)
18
Test.cpp (1 of 2) Use a function template to process Stack class-template specializations CS-2303, A-Term 2012 Templates (again)
19
Test.cpp (2 of 2) CS-2303, A-Term 2012 Templates (again)
20
Questions? CS-2303, A-Term 2012 Templates (again)
21
Non-type Parameters and Default Types for Class Templates
Nontype template parameters Can have default arguments Are treated as const Example Template header: template<typename T, int elements>... Declaration: Stack< double, 100 > salesFigures; Type parameters can have default arguments – example:– Template header: template< typename T = string >... Declaration: Stack<> jobDescriptions; CS-2303, A-Term 2012 Templates (again)
22
Templates and Inheritance
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 CS-2303, A-Term 2012 Templates (again)
23
Templates and Friends Assume:– template<typename T> class X
A function can be a friend of every class-template specialization instantiated from a class template friend void f1(); f1 is a friend of X<double>, X<string>, etc. A function can be the friend of only a class-template specialization with the same type argument friend void f2( X<T> & ); f2( X<float> & ) is a friend of X<float> but not a friend of X<string> CS-2303, A-Term 2012 Templates (again)
24
Templates and Friends (continued)
A member function of another class can be a friend of every class-template specialization instantiated from a class template friend void A::f3(); f3 of class A is a friend of X< double >, X< string >, etc. A member function of another class can be a friend of only a class-template specialization with the same type argument friend void C<T>::f4( X<T> & ); C<float>::f4( X<float> & ) is a friend of X<float> but not a friend of X<string> CS-2303, A-Term 2012 Templates (again)
25
Templates and Friends (continued)
Another class can be a friend of every class-template specialization instantiated from a class template friend class Y; Every member function of class Y is a friend of X<double>, X<string>, etc. A class-template specialization can be the friend of only a class-template specialization with the same type parameter friend class Z< T >; Class-template specialization Z<float> is a friend of X<float>, Z<string> is a friend of X<string>, etc. CS-2303, A-Term 2012 Templates (again)
26
Templates and static members
Each class-template specialization has own copy of each static data member All objects of that specialization share that one static data member static data members must be defined and, if necessary, initialized at file scope Each class-template specialization gets its own copy of the class template’s static member functions CS-2303, A-Term 2012 Templates (again)
27
Questions about Class Templates?
CS-2303, A-Term 2012 Templates (again)
28
Templates in the C++ Standard Library
Strings <basic_string> is the template class string is typedef basic_string<char> There are lots of other kinds of strings Japanese, Chinese, w_char, etc. Vector Generalization of one-dimensional array Includes (stack operations, etc.) vector<bool> is a specialization for one bit per bool item List Ditto CS-2303, A-Term 2012 Templates (again)
29
Templates in the C++ Standard Library (continued)
ValArray bitset Bitwise operations on bits Iterators For sequencing through collections of objects … CS-2303, A-Term 2012 Templates (again)
30
Questions? CS-2303, A-Term 2012 Templates (again)
31
CS-2303, A-Term 2012 Templates (again)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.