C++ Primer笔记 模板

2015-07-20 17:40:24 · 作者: · 浏览: 4

1 函数模板
template
T compare(const T & v1,const T &v2)
{
return ..
}


编译器承担了为每种类型编写函数的单调工作.


inline
template inline T min(const T&,const T&)


2 类模板
template
class Q{
T fun();
}


3. 形参屏蔽外部变量
模板形参不能在内部重新定义
模板的声明和定义名字可以不相同


4
template
Parm fcn(Parm& array,U value)
{
typename Parm::size_type *p;//不知道size_type是否为类型的时候最好加上typename
}


5 非类型模板形参
template
void array(T (&parm)[N]){}

调用:
int x[42];
array_init(x);// = array_init(int (&)[42]);


6 形参为引用时,数组不能转换为指针


7 显示指定实参


template
T3 func(T2,T1)
long func (int,long);


8 类外定义模板


template void Queue ::destroy(){}


9 类模板中的友元函数声明
template class Bar{
template friend class FooBar;
template friend void fun();
}


10 模板static 不同类型 不同的copy内存