50 likes | 172 Views
E81 CSE 532S: Advanced Multi-Paradigm Software Development. Introduction to Generic Programming in C++. Chris Gill Department of Computer Science Washington University, St. Louis cdgill@cs.wustl.edu. Each Paradigm Has a Particular View.
E N D
E81 CSE 532S: Advanced Multi-Paradigm Software Development Introduction to Generic Programming in C++ Chris Gill Department of Computer Science Washington University, St. Louis cdgill@cs.wustl.edu
Each Paradigm Has a Particular View “Computer programming is largely a matter of algorithms and data structures.” • Austern pp. 3 (Generic Programming Paradigm) • Object-based might say “methods and data” • OO might say “inheritance and polymorphism” • Patterns might say “design forces and contexts” • And so forth …
What is the STL? • An abstract library of concepts • A formal hierarchy of software requirements • A library of interchangeable modules • Algorithms • Containers • Iterators • Function objects, and much more • A flexible and efficient software framework • An extensible platform for software development
What is Generic Programming? • An abstraction technique for algorithms • Argument types are as general as possible • Separates algorithm steps & argument properties • What GoF design pattern does this resemble? • Type requirements can be specified, systematized • Can cluster requirements into abstractions • Termed “concepts” • Concepts can refine other concepts • Captures relationships between concepts • Analogy to inheritance hierarchies • A type that meets a concept’s requirements • Is a “model” of the concept • Can be plugged in to meet that set of requirements
Generic Algorithm 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; } • A generic algorithm • Searches any one-dimensional sequence of elements, for any type on which the operators work