











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An outline for lecture 14 of the CSE333 course at the University of X, focusing on C++ templates. The instructor, Hal Perkins, discusses the concept of parametric polymorphism and function templates. Students are introduced to the idea of writing 'generic code' that is type-independent and compile-time polymorphic. examples of template functions and compiler inference, as well as non-type templates. The lecture also covers class templates and their use in creating a pair class.
Typology: Study notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!












Administrivia v Homework 2 due tomorrow (10/25) § File system crawler, indexer, and search engine § Don’t forget to clone your repo to double-/triple-/quadruple-check
v Midterm: Friday, 11/2 in class § Closed book, no notes § Old exams and topic list on the course web now
Suppose that… v You want to write a function to compare two ints v You want to write a function to compare two strings § Function overloading! // returns 0 if equal, 1 if value1 is bigger, - 1 otherwise int compare (const int &value1, const int &value2) { if (value1 < value2) return - 1 ; if (value2 < value1) return 1 ; return 0 ; } // returns 0 if equal, 1 if value1 is bigger, - 1 otherwise int compare (const int &value1, const int &value2) { if (value1 < value2) return - 1 ; if (value2 < value1) return 1 ; return 0 ; } // returns 0 if equal, 1 if value1 is bigger, - 1 otherwise int compare (const string &value1, const string &value2) { if (value1 < value2) return - 1 ; if (value2 < value1) return 1 ; return 0 ; }
Hm… v The two implementations of compare are nearly identical! § What if we wanted a version of compare for every comparable type? § We could write (many) more functions, but that’s obviously wasteful and redundant v What we’d prefer to do is write “ generic code ” § Code that is type-independent § Code that is compile-type polymorphic across types
Function Templates v Template to compare two “things”: #include
Compiler Inference v Same thing, but letting the compiler infer the types: #include
What’s Going On? v The compiler doesn’t generate any code when it sees the template function § It doesn’t know what code to generate yet, since it doesn’t know what types are involved v When the compiler sees the function being used, then it understands what types are involved § It generates the instantiation of the template and compiles it (kind of like macro expansion)
This Creates a Problem #include
Solution # #include
Class Templates v Templates are useful for classes as well § (In fact, that was one of the main motivations for templates!) v Imagine we want a class that holds a pair of things that we can: § Set the value of the first thing § Set the value of the second thing § Get the value of the first thing § Get the value of the second thing § Swap the values of the things § Print the pair of things
Pair Function Definitions template
Using Pair #include
Review Questions (both template and class issues) v Why are only get_first() and get_second() const? v Why do the accessor methods return Thing and not references? v Why is operator<< not a friend function? v What happens in the default constructor when Thing is a class? v In the execution of Swap(), how many times are each of the following invoked (assuming Thing is a class)?