成员函数(member function)模板(template) 详解 及 代码

18 篇文章 0 订阅

成员函数(member function)模板(template) 详解 及 代码

 

本文地址: http://blog.csdn.net/caroline_wendy/article/details/16918085

 

成员模板(member template) 既可以在普通类(ordinary class), 也可以在类模板(class template);

在普通类中, 在使用成员函数时, 不用提供模板参数, 函数可以根据使用的参数,

自动推导(deduce)模板实参(template argument)对应模板形参(template parameter);

在类模板中, 成员函数的模板参数(template parameter)可以和类的模板参数不同, 但在定义(definition)中,

必须添加两个模板参数列表(template parameter list), 第一个为类的, 第二个为成员函数的;

代码如下:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * cppprimer.cpp 
  3.  * 
  4.  *  Created on: 2013.11.24 
  5.  *      Author: Caroline 
  6.  */  
  7.   
  8. /*eclipse cdt, gcc 4.8.1*/  
  9.   
  10. #include <iostream>  
  11. #include <functional>  
  12. #include <memory>  
  13. #include <algorithm>  
  14. #include <string>  
  15. #include <vector>  
  16. #include <deque>  
  17. #include <iterator>  
  18.   
  19. //函数模板默认参数  
  20. template <typename T, typename F = std::less<T>>  
  21. int compare (const T &v1, const T &v2, F f = F())  
  22. {  
  23.     if (f(v1, v2)) return -1;  
  24.     if (f(v2, v1)) return 1;  
  25.     return 0;  
  26. }  
  27.   
  28. class DebugDelete {  
  29. public:  
  30.     DebugDelete (std::ostream &s = std::cerr) : os (s) { }  
  31.     template <typename T> void operator() (T *p) const {  
  32.         os << "deleting unique_ptr" << std::endl; delete p;  
  33.     }  
  34. private:  
  35.     std::ostream &os;  
  36. };  
  37.   
  38. template <typename T> class Blob {  
  39. public:  
  40.     template <typename It> Blob (It b, It e);  
  41.     /*template <typename It> Blob (It b, It e) { 
  42.         std::sort(b, e); 
  43.     }*/  
  44. };  
  45.   
  46. template <typename T>  
  47. template <typename It>  
  48. Blob<T>::Blob (It b, It e) {  
  49.         std::sort(b, e); //容器需要允许被排序  
  50. }  
  51.   
  52. int main (void) {  
  53.   
  54.     std::cout << "compare (0, 42) = " << compare (0, 42) << std::endl;  
  55.   
  56.     double* p = new double;  
  57.     DebugDelete d;  
  58.     d(p); //使用时, 可以自动推倒模板  
  59.     int* ip = new int;  
  60.     DebugDelete() (ip);  
  61.     std::unique_ptr<int, DebugDelete> pi (new int, DebugDelete());  
  62.     std::unique_ptr<std::string, DebugDelete> ps (new std::string, DebugDelete());  
  63.   
  64.     int ia[] = {9, 8, 7, 6, 5};  
  65.     std::vector<long> vi = {5, 4, 3, 2, 1, 0};  
  66.     std::deque<std::string> w = {"lady""girl""woman""now"};  
  67.   
  68.     Blob<int> a1(std::begin(ia), std::end(ia));  
  69.     Blob<int> a2(vi.begin(), vi.end());  
  70.     Blob<std::string> a3(w.begin(), w.end());  
  71.   
  72.     std::cout << "int ia[] = ";  
  73.     for (const auto i : ia) { std::cout << i << " "; }  
  74.     std::cout << std::endl;  
  75.     std::cout << "std::vector<long> vi = ";  
  76.     for (const auto i : vi) { std::cout << i << " "; }  
  77.     std::cout << std::endl;  
  78.     std::cout << "std::list<const char*> w = ";  
  79.     for (const auto i : w) { std::cout << i << " "; }  
  80.     std::cout << std::endl;  
  81.   
  82.     return 0;  
  83.   
  84. }  


输出:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. deleting unique_ptr  
  2. deleting unique_ptr  
  3. deleting unique_ptr  
  4. deleting unique_ptr  
  5. compare (0, 42) = -1  
  6. int ia[] = 5 6 7 8 9   
  7. std::vector<long> vi = 0 1 2 3 4 5   
  8. std::list<const char*> w = girl lady now woman   


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值