-
suppose you need a list of X and a list of Y
- the lists would use similar code
- they differ by the type shored in the list
-
choices
- require common base class 方案1
- may not be desirable
- clone code 方案2
- preserves type-safety
- hard to manage 难以管理维护
- untyped lists 方案3 没有类型的list
- type unsafe 不安全
- require common base class 方案1
函数模板(用来做出函数) 类模板 模板函数 模板类
- reuse source code
- generic programming
- use types as parameters in class or function definitions 类型成为参数
- template functions
- example : sort function
- template classes
- example:contains such as stack,list, queue...
- stack operations are independent of the type of items in the stack
- template member functions
- example:contains such as stack,list, queue...
-
perform similar operations on different types of data
-
swap function for two int arguments:
void swap(int& x,int& y){ int temp = x; x = y; y = temp; } -
what if we want to swap flats ,strings,Currency,Person?
template// 声明而不是定义 void swap(T& x,T& y){ // 函数模板 T代表一个类型 T temp = x; x = y; y = temp; }
- the template keyword introduces the template
- the class T specifies a parameterized type name
- class means any built-in type or user-defined type
- inside the template,use T as a type name
- parameter types represent:
- types of arguments to the function
- return type of the function
- declare variables within the function
- generating a declaration from a template class/function and template arguments:
- types are substituted into template
- new body of function or class definition is created
- syntax errors,type checking
- specialization -- a version of a template for a particular argument(s)
int i = 3;
int j = 4;
swap(i,j); //use explicit int swap
float k = 4.5; float m = 3.7;
swap(k,m); //instanstiate float swap 编译器给制造出来了一个相应函数
std::string s("hello");
std::string t("world");
swap(s,t); //std::string swap
- a template function is an instantiation of a function template
-
only exact match on types is used
-
no conversion operations are applied
swap(int, int); //ok swap(double, double); //ok swap(int, double) //error
-
even implicit conversions are ignored
-
template functions and regular functions coexist
-
check first for unique function match
-
then check for unique function template match
-
then do overloading on functions
void f(float i,float k)(); template
void f(T t,T u){}; f(1.0,2.0); f(1,2); f(1,2.0)
-
the compiler deduces the template type from the actual arguments passed into the function
-
can be explicit:
-
for example,if the parameter is not in the function signature(older compilers won't allow this)
template
void foo(void){...} foo ();//type T is int foo ();//tyoe T is float
-
-
classes parameterized by types
- abstract operations form the types being operated upon
- define potentially infinite set of classes
- another step towards reuse
-
typical use:container classes
stack
//is a stack that is parameterized over int list queer
templateusageclass Vector{ public: Vector(int); ~Vector(); Vector(const Vector&); Vector& operator=(const Vector&); T& operator[] (int); private: T* m_elements; int m_size; };
VectorVector membersv1(100); Vertor v2(256); v1[20] = 10; v2[20] = v1[20]; //ok if int -> Comlex defined
template// 每一个都是函数模板 Vector ::Vector(int size):m_size(size){ m_elementds = new T[m_size]; } template T& Vertor ::operator[](int indx){ if(indx < m_size && indx > 0){ return m_elments[indx]; }else{ ... } }
类模板里面的每一个函数都是函数模板
a simple sort function//bubble sort -- don't use it templatesorting the vectorvoid sort(vector & arr){ // 函数模板 声明 const size_t last = arr.size()-1; for(int i = 0;i < last; i++){ for(int j = last; i < j; j--){ if(arr[j] < arr[j-1]){ //which swap? swap(arr[j], arr[j-1]); } } } }
vectortemplatesvi(4); vi[0] = 4;vi[2] = 7; vi[1]=3; vi[3] = 1; sort(vi); //sort(vector &) vector vs; vs.push_back("Fred"); vs.push_back("Willma"); vs.push_back("Barney"); vs.push_back("Dino"); vs.push_back("Prince"); sort(vs); //sort (vector &) //note:sort uses operator < for comparation
-
templates can use multiple types
template
class HashTable{ const Value& lookup(const Key&) const; void install(const Key& ,const Value&); ... }; -
templates nest ---they're just new types!
Vector< Vector
> //note sapce > > 空格 -
type arguments can be complicated
Vector
&,int)>//内层是函数
-
template arguments can be constant expressions
-
non-Type parameters
- can have a default argument
template
class FixedVector{ public: FixedVector(); //... T& oprator[](int); private: T elements[bounds];//fixed size array }
-
usage
FixedVector
v1; FixedVector v2; FixedVector v3;//uses default -
summary
- embedding sizes not necessarily
- can make code faster
- makes use more complicated
- size argument appears everywhere
- can lead to (even more) code bloat
-
templates can inherit from non-template classes
template
class Deried:public Base{...} -
templates can inherit form template classes
template
class Derived : pubic List{...} -
non-templates classes can inherit from templates
class SupervisorGroup:public List
- get a non-template version working first
- establish a good set of test cases
- measure performance and tune



