1 / 34

C++ Templates

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

mkarns
Télécharger la présentation

C++ Templates

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. C++ Templates

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

  3. 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

  4. Evolution of Reusability, Genericity Evolution of data features of programming languages Evolution of algorithmic features of programming languages • NiklausWirth (inventor of Pascal) • Stressed data and algorithms cannot be separated

  5. Function GenericityOverloading 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 functionvoid swap (int & first, int & second){ int temp = first; first = second; second = temp; } • Then callswap(x,y);

  6. Function GenericityOverloading 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?

  7. 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

  8. 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;}

  9. 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: template <typename T> void Swap(T &first,T &second) {Ttemp = first; first = second; second = temp;}

  10. 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 (< >).

  11. 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

  12. 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

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

  14. 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.

  15. 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.

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

  17. 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()

  18. 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

  19. 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; } template <typename ElementType> 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;}

  20. 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’ }

  21. Continued 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 } Exercise: Write a template function that finds and returns the largest element stored in an array. You may assume the elements of the array are comparable via the relational ops.

  22. #include <iostream> using namespace std; const unsigned int SIZE = 25; template <typename ElementType> T max(const ElementType list[]); int main() { float array[SIZE]; //initialize array with some values cout << max(array) << endl; } template <typename ElementType> T max(const ElementType list[]) { T largest = list[0]; for(unsigned int i = 1; i < SIZE; i++) if (list[i] > largest) largest = list[i]; return largestt; }

  23. 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.

  24. C++ class templates (.h) template<class T> classItem { public: Item() ; voidSetData(const T &nValue); T GetData() const; friendostream & operator<<(ostream &out, constItem<T> & it); private: T Data; }; template<class T> Item<T> :: Item() : Data( T() ) {} template<class T> voidItem<T> :: SetData(const T &nValue) { Data = nValue } template<class T> T Item<T> :: GetData() const { return Data; } template<class T> ostream & operator<<(ostream &out, constItem<T> & it) { out << it.Data; return out; }

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

  26. Rules For Class Templates • Implementations of member functions outside class declarationmust be function templates (as in .h of slide 24). • All uses of class name as atype must be parameterized (see underlined items in slide 24). • Member functions must be definedin the same file as the class declaration.

  27. 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 bytemplate<class T>

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

  29. Applying the Rules to Friend Functions • Consider the addition of a friend functionoperator<< • Second parameter is of type Item, must be parameterized friend ostream & operator<<(ostream & out,const Item<T>& it);

  30. 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, constItem<T> & it) { out << it.Data; return out; }

  31. Notes • Templates may have more than one type parameter template<class T, class S> typefunctionNme(parameters); • May also have ordinary value parameters template<class T, intSize> typefunctionNme(parameters);

  32. 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

  33. Standard Template Library • Example of a specific • container class • iterator • algorithm

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

More Related