Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of C++ Templates

Similar presentations


Presentation on theme: "Overview of C++ Templates"— Presentation transcript:

1 Overview of C++ Templates
Templates are used to parameterize classes and functions with different types Function templates do not require explicit declaration of the parameterized types when called Classes require explicit declaration of parameterized types when instantiated Some compilers require that template definitions be included with declarations – others do not

2 Function Templates template <typename T> void swap(T & lhs, T & rhs) { T temp = lhs; lhs = rhs; rhs = temp; } int main () { int i = 3; int j = 7; swap (i, j); // i is now 7, j is now 3 return 0; Function template swap takes a single type parameter, T interchanges values of two passed arguments of the parameterized type Compiler instantiates the function template with type int in main Note that the compiler infers that the type is int when swap is called Based on the types of the arguments i and j

3 Function Template Example
STL linear search (based on Austern pp. 13): template <typename Iterator, typename T> Iterator find2 (Iterator first, Iterator last, const T & value) { while (first != last && *first != value) { ++first; } return first; Our first generic algorithm Searches any one-dimensional sequence of elements “Not found” is a normal result, does not throw exception Returns position last: just past the end of the range [first, last)

4 More about Concepts and Models
A concept defines type requirements A predicate to classify/categorize types Any specific type that meets the requirements is a model of that concept What requirement(s) does the expression return first; impose? What about while(first != last && *first != value) { ++first; } Iterator const T &

5 Interface Polymorphism: Refinement
A concept C is a refinement of concept D if C imposes all of the requirements of D Modeling and refinement satisfy three formal properties Reflexivity: A concept refines itself Containment: if T models C and C refines D then T models D Transitivity: If C refines D then C refines any concept D refines What in the OO paradigm is this like? How does it differ? C0 transitivity C1 T1 T2 C2 containment T3 T4 can substitute, e.g., T3 for T1

6 Class Templates Start with a simple declaration
template <typename T> class Array { public: Array(const int size); ~Array(); const int size () const; private: T * m_values; const int m_size; }; int main (int, char**) { Array <int> a(10); return 0; } Start with a simple declaration Which we’ll expand as we go Notice that parameterized type T is used within the class template Type of pointer to array When an instance is declared, must also explicitly specify the concrete type parameter E.g., int in function main (int, char**)

7 Class Templates and Inheritance
class ArrayBase { public: ArrayBase(const int size); ~ArrayBase(); const int size () const; protected: const int m_size; }; template <typename T> class Array : public ArrayBase { Array(const int size); ~Array(); private: T * m_values; Class templates can inherit from base classes Or class templates, but must relate type parameters to those of base Here, we’ve separated template and non-template code Non-template base class Template derived class Functions and variables that do not need to refer to type parameters go into the base class This is useful in many cases To avoid accidental type errors (ArrayBase doesn’t access T) To reduce program size (separate method definitions compiled for Array<int> Array<float> etc.)

8 One Last Detail: Associated Types
template <typename T> class Array : public ArrayBase { public: typedef T* iterator; Array(const int size); ~Array(); iterator begin () { return m_values;} iterator end () { return m_values+m_size;} private: T * m_values; }; void foo { Array<int> a; fill_array(a); for (Array<int>::iterator i = a.begin(); i != a.end(); ++i) { cout << *i << “ ” << endl; } The exact type of a type parameter is not known The compiler can work around that in many cases But sometimes it needs more information Additional information can be associated with a type For example, a class template can define an iterator type Available to the template and code that uses the template More about how this is done (and why) in later lectures

9 Tips on Templates The set of requirements that a class template or function template places on its parameterized types is called a concept in generic programming terminology Any type that meets those requirements is said to model the concept, and can be used in that template Concept refinement supports interface polymorphism Push common code and variables up into non-template base classes Can help avoid subtle type errors Can help reduce the size of the executable program Associated types expand the power of templates E.g., used extensively in the C++ Standard Template Library

10 For Next Time Topic No assigned readings Template Examples
Please catch up if you’ve not done so already


Download ppt "Overview of C++ Templates"

Similar presentations


Ads by Google