Download C++ Templates: Parametric Polymorphism and Function Templates in CSE333, Autumn 2018 and more Study notes C programming in PDF only on Docsity! CSE333, Autumn 2018L14: C++ Templates C++ Templates CSE 333 Autumn 2018 Instructor: Hal Perkins Teaching Assistants: Tarkan Al-Kazily Renshu Gu Travis McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu CSE333, Autumn 2018L14: C++ Templates 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 compilation, execution, and tests! • If your code won’t build or run when we clone it, well, you should have caught that… v Midterm: Friday, 11/2 in class § Closed book, no notes § Old exams and topic list on the course web now • Everything up through C++ classes, dynamic memory, templates & STL § Review in sections next week v No new exercises due until after hw1 due 2 CSE333, Autumn 2018L14: C++ Templates 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 5 CSE333, Autumn 2018L14: C++ Templates C++ Parametric Polymorphism v C++ has the notion of templates § A function or class that accepts a type as a parameter • You define the function or class once in a type-agnostic way • When you invoke the function or instantiate the class, you specify (one or more) types or values as arguments to it § At compile-time, the compiler will generate the “specialized” code from your template using the types you provided • Your template definition is NOT runnable code • Code is only generated if you use your template 6 CSE333, Autumn 2018L14: C++ Templates Function Templates v Template to compare two “things”: 7 #include <iostream> #include <string> // returns 0 if equal, 1 if value1 is bigger, -1 otherwise template <typename T> // <...> can also be written <class T> int compare(const T &value1, const T &value2) { if (value1 < value2) return -1; if (value2 < value1) return 1; return 0; } int main(int argc, char **argv) { std::string h("hello"), w("world"); std::cout << compare<int>(10, 20) << std::endl; std::cout << compare<std::string>(h, w) << std::endl; std::cout << compare<double>(50.5, 50.6) << std::endl; return 0; } functiontemplate.cc CSE333, Autumn 2018L14: C++ Templates 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) • The compiler generates template instantiations for each type used as a template parameter 10 CSE333, Autumn 2018L14: C++ Templates This Creates a Problem 11 #include <iostream> #include "compare.h" using namespace std; int main(int argc, char **argv) { cout << comp<int>(10, 20); cout << endl; return 0; } #include "compare.h" template <typename T> int comp(const T& a, const T& b) { if (a < b) return -1; if (b < a) return 1; return 0; } #ifndef _COMPARE_H_ #define _COMPARE_H_ template <typename T> int comp(const T& a, const T& b); #endif // _COMPARE_H_ compare.h compare.cc main.cc CSE333, Autumn 2018L14: C++ Templates Solution #1 12 #include <iostream> #include "compare.h" using namespace std; int main(int argc, char **argv) { cout << comp<int>(10, 20); cout << endl; return 0; } #ifndef _COMPARE_H_ #define _COMPARE_H_ template <typename T> int comp(const T& a, const T& b) { if (a < b) return -1; if (b < a) return 1; return 0; } #endif // _COMPARE_H_ compare.h main.cc CSE333, Autumn 2018L14: C++ Templates Pair Class Definition 16 #ifndef _PAIR_H_ #define _PAIR_H_ template <typename Thing> class Pair { public: Pair() { }; Thing get_first() const { return first_; } Thing get_second() const { return second_; } void set_first(Thing ©me); void set_second(Thing ©me); void Swap(); private: Thing first_, second_; }; #include "Pair.cc" #endif // _PAIR_H_ Pair.h CSE333, Autumn 2018L14: C++ Templates Pair Function Definitions 17 template <typename Thing> void Pair<Thing>::set_first(Thing ©me) { first_ = copyme; } template <typename Thing> void Pair<Thing>::set_second(Thing ©me) { second_ = copyme; } template <typename Thing> void Pair<Thing>::Swap() { Thing tmp = first_; first_ = second_; second_ = tmp; } template <typename T> std::ostream &operator<<(std::ostream &out, const Pair<T>& p) { return out << "Pair(" << p.get_first() << ", " << p.get_second() << ")"; } Pair.cc CSE333, Autumn 2018L14: C++ Templates Using Pair 18 #include <iostream> #include <string> #include "Pair.h" int main(int argc, char** argv) { Pair<std::string> ps; std::string x("foo"), y("bar"); ps.set_first(x); ps.set_second(y); ps.Swap(); std::cout << ps << std::endl; return 0; } usepair.cc