Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233271
  • 博文数量: 127
  • 博客积分: 34
  • 博客等级: 民兵
  • 技术积分: 655
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-03 10:53
文章分类

全部博文(127)

文章存档

2013年(19)

2012年(108)

分类:

2012-11-04 22:34:40

原文地址:C++ Templates--函数模板 作者:LanceRen



今天重新温习C++ Template,觉得程序这东西还是要经常的写才好,才能深入理解,才不容易忘记。
 
Ok,闲话少说,进正题。。。
 
觉得学习东西有一个很好的步骤,就是先提问:
1.What is The Template?
2.Why Template?
3.How Template?
 
 
函数模板
What's the Template?
1.函数模板:函数模板提供了一种函数行为,该函数支持多种不同参数类型的调用。

  1. //basics/max.hpp
  2. template <typename T>
  3. inline T const& max(T const& a,T const& b)
  4. {
  5.     return a<b?b:a;
  6. }
注:关键字typename 可以用class关键字替代。
 
模板的编译
如果试图基于一个不支持模板内部所使用操作的类型实例化一个模板,那么将会导致一个编译期错误。

  1. std::complex<float> c1, c2; // doesn't provide operator <

  2. max(c1,c2); // ERROR at compile time
于是我们得出结论:模板被编译两次
1.实例化之前,先检查模板代码本身,查看语法是否正确。(如,遗漏分号等)
2.在实例化期间,检查模板代码,查看是否所有调用都有效。(如,实例化类型不支持某些函数的调用等)

注:与普通函数编译链接有区别,普通函数编译时,只要引用的头文件中有函数的声明,编译就能通过。
 
模板调用
1.对实参进行强制类型转换,使它们可以相互匹配
max(static_cast(4),4,2);
2.显示指定T的类型
max(4,4.2)
3.指定两个参数可以具有不同的数据类型
template >
 
模板重载
  1. // basics/max2.cpp

  2. // maximum of two int values
  3. inline int const& max (int const& a, int const& b)
  4. {
  5.     return a<b?b:a;
  6. }

  7. // maximum of two values of any type
  8. template <typename T>
  9. inline T const& max (T const& a, T const& b)
  10. {
  11.     return a<b?b:a;
  12. }

  13. // maximum of three values of any type
  14. template <typename T>
  15. inline T const& max (T const& a, T const& b, T const& c)
  16. {
  17.     return max (max(a,b), c);
  18. }

  19. int main()
  20. {
  21.     ::max(7, 42, 68); // calls the template for three arguments
  22.     ::max(7.0, 42.0); // calls max<double> (by argument deduction)
  23.     ::max('a', 'b'); // calls max<char> (by argument deduction)
  24.     ::max(7, 42); // calls the nontemplate for two ints
  25.     ::max<>(7, 42); // calls max<int> (by argument deduction)
  26.     ::max<double>(7, 42); // calls max<double> (no argument deduction)
  27.     ::max('a', 42.7); // calls the nontemplate for two ints
  28. }
普通函数可以与包含它实例化形式的模板函数共同存在,可以被不同形式的语句调用,上面main中第三句调用的就是普通函数,而第五句就是调用的模板函数。
 
模板函数的嵌套调用

  1. // basics/max3a.cpp

  2. #include <iostream>
  3. #include <cstring>
  4. #include <string>

  5. // maximum of two values of any type (call-by-reference)
  6. template <typename T>
  7. inline T const& max (T const& a, T const& b)
  8. {
  9.     return a < b ? b : a;
  10. }

  11. // maximum of two C-strings (call-by-value)
  12. inline char const* max (char const* a, char const* b)
  13. {
  14.     return std::strcmp(a,b) < 0 ? b : a;
  15. }

  16. // maximum of three values of any type (call-by-reference)
  17. template <typename T>
  18. inline T const& max (T const& a, T const& b, T const& c)
  19. {
  20.     return max (max(a,b), c); // error, if max(a,b) uses call-by-value
  21. }

  22. int main ()
  23. {
  24.     ::max(7, 42, 68); // OK

  25.     const char* s1 = "frederic";
  26.     const char* s2 = "anica";
  27.     const char* s3 = "lucas";
  28.     ::max(s1, s2, s3); // ERROR 错误产生

  29. }
发生错误:
对于3个C-String 调用max(),那么语句产生
return max(max(a,b),c)
这里max(a,b) 讲产生一个c的新的临时的局部值,返回则返回该临时值得引用,无效的引用。
 

  1. // basics/max4.cpp

  2. // maximum of two values of any type
  3. template <typename T>
  4. inline T const& max (T const& a, T const& b)
  5. {
  6.     return a<b?b:a;
  7. }

  8. // maximum of three values of any type
  9. template <typename T>
  10. inline T const& max (T const& a, T const& b, T const& c)
  11. {
  12.     return max (max(a,b), c); // uses the template version even for ints
  13. } // because the following declaration comes
  14.                                // too late:
  15. // maximum of two int values
  16. inline int const& max (int const& a, int const& b)
  17. {
  18.     return a<b?b:a;
  19. }
在这段语句中,三个参数模板调用的也是模板函数。
 

 
 
阅读(399) | 评论(0) | 转发(0) |
0

上一篇:链表(C++实现)

下一篇:查找算法

给主人留下些什么吧!~~