C++_templates_and_interface_polymorphism_II

C++_templates_and_interface_polymorphism_II - Templates...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters Types that are plugged in must meet those requirements Otherwise, the code won’t compile (and errors will say why) The set of requirements imposed is called a concept 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; } const T & Iterator
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Interface Polymorphism: Concept 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 C1 C2 T1 T2 T3 T4 C0 containment transitivity can substitute, e.g., T3 for T1
Background image of page 2
CSE 332: C++ template examples 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; } } A type parameter is not concrete until its instantiated The compiler can work around that in many cases But sometimes the compiler 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

C++_templates_and_interface_polymorphism_II - Templates...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online