STL_2: 模板_2_函数模板
2015-12-16 00:14
369 查看
tempalte<typename T> const T& min(const T& a,const T& b) {return (a<b)? a:b;} int z=min<int>(i,j);
min(i,j)—-用int特化
函数模板的类型推断
tempalte<typename T,typename U> const T& min(const T& a,const U& b) {return (a<b)? a:b;} int z=min<int.double>(i,j);
函数模板的重载
template<typename T> const T& min(const T& a,const T& b) //min(1,2)--自动类型推断 {return (a<b)? a:b;} const char* min(const char *a,const char* b) {return strcmp(a,b)<0 ? a:b;} double min(double x,doule y) //min(1.0,2.0) {return (x<y)?x:y}
生成的函数模板地址作为参数
template<typename T> void f(T*) {} void h(void (*pf)(int *)) {} template <typename T> void g(void (*pf)(T*)) {} int main() { h(&f<int>); f(&f); //类型推导 g<int>(&f<int>); g(&f<int>); g<int>(&f); }
函数模板的半有序
//优先级顺序从上到下变低 template<class T> void f(T); template<class T> void f(T*); template<class T> void f(const T*);
模板特化
显示特化
tempalte<class T> const T& min(const T &a,const T &b) {.....} ---- //特化(const char*) tempalte<> const char* const& min<const char*> (const char* const &a,const char* const &b) {.....} min(s1,s2); min<>(s1,s2);
<\lt const char*>-特化类型;用const char* 代替 T
半特化
类模板—半特化代码的膨胀
相关文章推荐
- android-Hardware Acceleration
- hdu 1301 Jungle Roads (最小生成树)
- React 万能的函数表达式
- 使用Promise解决多层异步调用的简单学习
- An Important Pointer Concept
- strcpy,strcmp,strcat,strlen的实现以及大概介绍(下)
- Java 不定长度参数
- java设计优化--装饰者模式
- oracle ora-00054:resource busy and acquire with nowait specified解决方法
- 合并字符串(动态内存申请)
- 做个备忘(linux重装重要文件)
- 程序之路的开始
- jQuery中ready与load事件的区别
- [傅里叶变换及其应用学习笔记] 十三. 分布的傅里叶变换
- HSRP
- java多线程网络服务器端向客户端发送文件
- jersy服务,将图片发送另个服务器,再将异步返回
- Thundering Herd Problem --- From Wiki
- Nullability and Objective-C
- Android中手机震动vibrate的实现