C++ recap: Templates
Function Templates
#include <iostream> template <typename T> T max (T x, T y) { return (x > y) ? x : y; } int main() { std::cout << max(10, 20) << std::endl; std::cout << max('a', 'y') << std::endl; return 0; }
Template Classes
#include <iostream> template <typename T> class Adder { public: Adder(); T add (T x, T y); }; template <typename T> Adder<T>::Adder() { // constructor here } template <typename T> T Adder<T>::add(T x, T y) { return x + y; } int main() { Adder<double> d; std::cout << d.add (1.11, 1.11) << std::endl; Adder<std::string> s; std::cout << s.add("hello ", "world") << std::endl; return 0; }