Template Prof. Jinkyu Jeong (Jinkyu@skku.edu) TA -- Minwoo Ahn (minwoo.ahn@csl.skku.edu) TA -- Donghyun Kim (donghyun.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu)
What is Template? Allows functions and classes to operate with generic types Function template Class template int Add(int n1, int n2) float Add(float n1, float n2) double Add(double n1, double n2)... template <typename T> T Add(T n1, T n2) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 2
Function Template
Function Template Basic terminology Function template template <typename SomeType> Return_type Function_name(Function_parameter)... Template function Template parameter Return_type Function_name<int>(Function_parameter) Return_type Function_name<char>(Function_parameter)... Instantiation(by compiler) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 4
Instantiation Compiler generate multiple versions of a function by template parameter Template parameter may be omitted Function_name<Template_parameter>(Function_parameter) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 5
Example template <typename T> = <class T> T Add(T num1, T num2) return num1 + num2; int main(void) cout << Add<int>(15, 20) << endl; cout << Add<double>(2.9, 3.7) << endl; cout << Add<int>(3.2, 3.2) << endl; 35 6.6 6 cout << Add(15, 20) << endl; cout << Add(2.9, 3.7) << endl; cout << Add(3.2, 3.2) << endl; Template function can be generated without template parameter 35 6.6 6.4 SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 6
Multiple Template Parameters template <class RT, class T1, class T2> RT Max(T1 a, T2 b) return a > b? a: b; int main(void) cout << Max<double, int, double>(3, 4.5) << endl; cout << Max<int, int, double>(3, 4.5) << endl; cout << Max<double>(3, 4.5) << endl; cout << Max<int>(3, 4.5) << endl; 4.5 4 4.5 4 SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 7
Overloading template <class T> T Max(T a, T b) return a > b? a: b; template <class T> T Max(T a, T b, T c) T temp = (a > b? a: b); return temp > c? temp: c; int main(void) cout << Max(1, 2) << endl; cout << Max(1, 2, 3) << endl; SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 8
Specialization Specialize all template parameters template <class T> T Max(T a, T b) return a > b? a: b; template <> Can be omitted string Max<string>(string a, string b) return a.length() > b.length()? a: b; int main(void) string s1 = abc ; string s2 = abcd ; cout << Max(11, 12) << endl; cout << Max(s1, s2) << endl; SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 9
Overloading vs. Specialization template <class T> void foo(t); template <class T> void foo(t*); template <> void foo<int>(int*) template <class T> void foo(t); template <class T> void foo(t*); //(a)basic template Overloading //(b)basic template //(c)specialization-(b) //(a)basic template Overloading //(b)basic template int *p foo(p) What function is called? template <> //(c)specialization-(a) void foo<int*>(int*) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 10
Class Template 11
Class Template Basic terminology Class template Template parameter template <typename SomeType> class Class_name... ; Instantiation(by compiler) Template class class Class_name<int>... class Class_name<char>...... SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 12
Instantiation Compiler generate multiple versions of a function by template parameter Template parameter must be explicit Class_name<Template_parameter> variable_name SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 13
Example template <typename T> class Point private: T xpos, ypos; public: Point(T x=0, T y=0)xpos = x; ypos = y; void ShowPosition() cout << x: << xpos <<, y: << ypos << endl; ; int main(void) Point<int> p1(3, 4); Point<double> p2(3.3, 4.2); p1.showposition(); p2.showposition(); SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 14
Member Function of Class Template Member function of class template can be defined outside the class template <typename T> class Point private: T xpos, ypos; public: Point(T x=0, T y=0)xpos = x; ypos = y; void ShowPosition(); ; template <typename T> void Point<T>::ShowPosition() cout << x: << xpos <<, y: << ypos << endl; SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 15
Member Function Overloading template <typename T> void Point<T>::ShowPosition() cout << x: << xpos <<, y: << ypos << endl; template <typename T> void Point<T>::ShowPosition(T dx, T dy) cout << x: << xpos+dx <<, y: << ypos+dy << endl; SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 16
Specialization Not only the member functions of a class template, class templates can also be specialized template <typename T> class Point... Point(T x=0, T y=0)xpos = x; ypos = y;... ; template <> class Point<string>... Point(string x= 0, string y= 0 )xpos = x; ypos = y;... ; SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 17
Partial Specialization Partial specialization is possible for class template unlike function template template <class T1, class T2> class Student private: T1 name; T2 score; ; template <typename T1> class Student<T1, int> private: T1 name; int score; ; int main(void) Student<string, int> s1; SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 18
Static Variable Static variable is independent of each instance template <class T> class StaticTest private: static T mem; public: void Add(int num) mem+=num; void Show() cout << mem << endl; template <class T> T StaticTest<T>::mem = 0; int main(void) StaticTest<int> st1; StaticTest<int> st2; st1.add(2); st2.add(3); st1.show(); StaticTest<long> st3; StaticTest<long> st4; st3.add(10); st4.show(); SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 19
[Lab Practice #1] Make Sort function by template Number of input sequence: 10 Available input types: int, float, double, char, string Sorting standard int, float, double: descending order char: descending order in ASCII code string: dictionary order SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong (jinkyu@skku.edu) 20