Template. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Similar documents
Introduction to C++ SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Class (Inheritance) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Git. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Class. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Project #1: NAND Simulator

Introduction to the OpenSSD Jasmine Platform

TEMPLATE IN C++ Function Templates

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Inheritance and Overloading. Week 11

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

SSE3052: Embedded Systems Practice

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

CS 2530 INTERMEDIATE COMPUTING

Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

SSE3052: Embedded Systems Practice

SSE2034: System Software Experiment 3

Darshan Institute of Engineering & Technology for Diploma Studies

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

CS11 Introduction to C++ Fall Lecture 6

Developed By : Mrs. K. M. Sanghavi

Constructor - example

COMP 2355 Introduction to Systems Programming

SSE2034: System Software Experiment 3 Spring 2016

Relaxed Memory Consistency

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

File System Implementation

Introduction to C++ Templates and Exceptions. C++ Function Templates C++ Class Templates Exception and Exception Handler

Advanced C++ Topics. Alexander Warg, 2017

Representing Integers

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

EECS402 Lecture 02. Functions. Function Prototype

LAB 4.1 Relational Operators and the if Statement

Advanced Processor Architecture

Implementing Abstract Data Types (ADT) using Classes

Object Oriented Design

SSE3052: Embedded Systems Practice

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Data Type Fall 2014 Jinkyu Jeong

Assembly IV: Complex Data Types

C:\Temp\Templates. Download This PDF From The Web Site

Comp151. Function Templates & Class Templates

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

TEMPLATES AND EXCEPTION HANDLING

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

C++ Programming Fundamentals

2 2

Logistics. Templates. Plan for today. Logistics. A quick intro to Templates. A quick intro to Templates. Project. Questions? Introduction to Templates

Binding and Variables

Lecture 6: Generics. Lisa (Ling) Liu

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Review: C++ Basic Concepts. Dr. Yingwu Zhu

SHREE RAMCHANDRA COLLEGE OF ENGINEERING. Laboratory Manual

Manipulating Integers

Constructors & Destructors

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Module Operator Overloading and Type Conversion. Table of Contents

C++ (classes) Hwansoo Han

Programming Abstractions

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Processor Architecture

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

BEng (Hons) Telecommunications. Examinations for 2016 / Semester 2

SSE3044: Operating Systems

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

W3101: Programming Languages C++ Ramana Isukapalli

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Software Development with C++ Templates

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Introduction to Computer Science Midterm 3 Fall, Points

Basic Templates Intro

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

type name(arg1, arg2,...) { body return }

Fall 2017 CISC124 9/16/2017

CPSC 427: Object-Oriented Programming

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

COEN244: Class & function templates

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CLASSES AND OBJECT CHAPTER 04 CLASS XII

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

FORM 2 (Please put your name and form # on the scantron!!!!)

Largest Online Community of VU Students

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

EECS 183 Week 8 Diana Gage. www-personal.umich.edu/ ~drgage

Abstract Data Types (ADT) and C++ Classes

Transcription:

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