Upload
pranshu-jaiswal
View
215
Download
0
Embed Size (px)
Citation preview
8/4/2019 comp322_10
1/22
Templates &Generic Programming
Junaed Sattar
November 12, 2008Lecture 10
8/4/2019 comp322_10
2/22
Generic Programming
Algorithms are written independentlyof data
data types are filled in during execution
functions or classes instantiated with datatypes
formally known as specialization
Also used in Metaprogramming
used by a compiler to generate temporary source
code
8/4/2019 comp322_10
3/22
Lets motivate ourselves
Number of algorithms are data-typeindependent
sorting
searching
finding n-th largest
swapping etc
Write once, use many times philosophy
dramatically reduces LOC (lines-of-code)
makes code much easier to maintain
8/4/2019 comp322_10
4/22
In C++
Templates = generic programming
Two types
function templates
special functions that can operate with generic types.
class templates
can have members that use template parameters as
types
8/4/2019 comp322_10
5/22
Function Templates
special functions using template types.
A template parameter is a special kind ofparameter used to pass a type as argument
just like regular function parameters
but pass types to a function
8/4/2019 comp322_10
6/22
Declaration Format?
format for declaring function templates with typeparameters
template function_ declaration;
template function_ declaration;
Same functionality, different keywords
use
older compilers only used format
8/4/2019 comp322_10
7/22
Example
create a template function that returns thegreater one of two objects
template myType GetMax (myType a, myType b){return (a>b?a:b);}
8/4/2019 comp322_10
8/22
Usage
int main () {
int i=5, j=6, k;long l=10, m=5, n;
k=GetMax(i,j);n=GetMax(l,m);cout
8/4/2019 comp322_10
9/22
Invalid Usage
int main () {
int i=5, j=6, k;long l=10, m=5, n;
k=GetMax(i,l);n=GetMax(j,m);}
One template type only in definition
call mixes int and long types
8/4/2019 comp322_10
10/22
Multiple template types
define template types after the templatekeyword
template myType GetMax (T1 a, T2 b){return (a>b?a:b);
}
no difference in function call
programmer must ensure sane algorithm
8/4/2019 comp322_10
11/22
Class templates
classes can have members that use templateparameters as type
template class mypair {T values [2];
public:mypair (T first, T second){values[0]=first; values[1]=second;
}};
8/4/2019 comp322_10
12/22
To use
stores two elements of any valid type
to store two integer values of type int with thevalues 115 and 36:
mypair myobject (115, 36);
to store two doubles:
mypair mydubs (3.0, 2.18);
8/4/2019 comp322_10
13/22
Non-inline definition
to define a function member outside thedeclaration of the class template, alwaysprecede that definition with the template
prefixtemplate class mypair {
T values [2];public:mypair (T first, T second){values[0]=first; values[1]=second;
}T GetMax();
};
8/4/2019 comp322_10
14/22
Continued
template T mypair::getmax (){T retval;retval = a>b? a : b;
return retval;}
int main () {mypair myobject (100, 75);
cout
8/4/2019 comp322_10
15/22
Why so many T's??
There are three T's in this declaration
first one is the template parameter.
second T refers to the type returned by thefunction
third T (the one between angle brackets)specifies that this function's template parameter
is also the class template parameter.
8/4/2019 comp322_10
16/22
Specialization
Why and what?
to define a different implementation for a templatewhen a specific type is passed as template
parameter explicitly declare a specialization of that
template
8/4/2019 comp322_10
17/22
Sample Case
A class with a sort method
sorts ints, chars, doubles, floats
also need to sort strings based on length, but the
algorithm is different not lexicographic sorting
Need to explicitly create template specializationfor the sort method when string is passed astype
8/4/2019 comp322_10
18/22
Code
template class MyContainer {
T element[100];public:
MyContainer( T *arg ){...};void Sort() {
// use your favorite sorting algorithm}
};
8/4/2019 comp322_10
19/22
Code
// class template specialization:template class MyContainer {
string element[100];
public:MyContainer (string *arg) {...}void Sort() {
// use a string-length based sort here}
};
8/4/2019 comp322_10
20/22
Non-type parameters?
templates can also have regular typedparameters, similar to those found in functions
template class mysequence {T memblock [N];
public:void setmember (int x, T value);T getmember (int x);
};
8/4/2019 comp322_10
21/22
Continued
template T mysequence::getmember (int x) {return memblock[x];
}
int main () {mysequence myints;mysequence myfloats;myints.setmember (0,100);
myfloats.setmember (3,3.1416);cout
8/4/2019 comp322_10
22/22
Next Class
STL
Exceptions and namespaces