Typelist 笔记


分类: C++&&算法&&设计模式   930人阅读  评论(0)  收藏  举报

以前看Typelist还没有真的理解,现在算理解了。

不过觉得Loki技巧过于华丽,也就是语言技巧。有些不实用,还是Boost更实用。

今天转移到boost库去了,这是昨天的笔记。

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <string>  
  4. #include <typeinfo>  
  5. //#include <loki/SmartPtr.h>  
  6. // class Text{  
  7. // public:  
  8. //   void output(){  
  9. //      cout<<"Hello,world!"<<endl;  
  10. //  }  
  11. // };  
  12. //“看在上帝的份上,从简单开始吧”——《具体数学》  
  13. template <typename T, typename N>  
  14. struct typelist{  
  15.     typedef T head;  
  16.     typedef N tail;  
  17. };  
  18. struct NULLType;  
  19. typedef typelist<char,typelist<char,typelist<float,typelist<int, NULLType> > > > typelist1;  
  20. //计算长度  
  21. template<typename typelist>  
  22. struct length;  
  23. template<>  
  24. struct length<NULLType>{  
  25.     enum{ len = 0};  
  26. };  
  27. //假如以typelist<char, typelist<string, NULLType>> 为例  
  28. //  
  29. template<typename T, typename N>  
  30. struct length< typelist<T,N> >{//这儿的N就是typelist<string,NULLType>  
  31.     enum{len = 1+ length<N>::len};//递归调用上面,直到遇到一个特化版本  
  32. };  
  33. //索引访问,下标是从0开始。  
  34. //这儿是模板参数的声明,决定模板参数的个数  
  35. //也就是后来的typelist<...>的参数个数  
  36. template<typename typelist, int i>//这儿表明有两个模板参数  
  37. struct TypeAt;  
  38. //这儿的i是从最左边的开始数起  
  39. template<typename T, typename N,int i>  
  40. struct TypeAt<typelist<T,N>,i>{//参数格式是2,分别是typelist<T,N>、i  
  41.     typedef typename TypeAt<N,i-1>::type type;  
  42. };  
  43. template<typename T, typename N>  
  44. struct TypeAt<typelist<T,N>, 0 >{  
  45.     typedef T type;  
  46. };  
  47. //查找typelist  
  48. //只考虑了正确的情况,没有容错功能  
  49. template<typename T, typename N>   
  50. struct IndexOf;  
  51. template<typename T, typename N>  
  52. struct IndexOf<typelist<T,N>, T>{  
  53.     enum{index = 0};  
  54. };  
  55. template<typename T, typename N, typename V> //  
  56. struct IndexOf<typelist<T,N>, V>{  
  57.       
  58.     enum {index = 1 + IndexOf<N,V>::index};  
  59. };  
  60. //附加元素到typelist  
  61. template<typename T, typename N>  
  62. struct Append;  
  63. template<typename T, typename N, typename V>  
  64. struct Append<typelist<T,N>, V>{  
  65.   typedef typelist<V, typelist<T,N> >  Result;  
  66. };  
  67. //erase  
  68. template<typename T, typename N>  
  69. struct Erase;  
  70. template<typename T, typename N>  
  71. struct Erase<typelist<T,N>,T>{  
  72.     typedef N Result;  
  73. };  
  74. template<typename T, typename N, typename C>  
  75. struct Erase<typelist<T,N>,C>{  
  76. private:  
  77.     typedef typename Erase<N, C>::Result Ret;  
  78. public:  
  79.     typedef typelist<T,Ret> Result;  
  80. };  
  81. //eraseall 查处全部相同的元素。  
  82. template<typename T, typename N>  
  83. struct EraseAll;  
  84. template<typename T>  
  85. struct EraseAll<NULLType, T>{  
  86.     typedef NULLType Result;  
  87. };  
  88. template<typename T, typename N>  
  89. struct EraseAll<typelist<T, N>,T>{  
  90.     typedef N Result;  
  91. };  
  92. template<typename T, typename N, typename V>  
  93. struct EraseAll<typelist<T, N>, V>{  
  94.     typedef typelist<T, typename EraseAll<N,V>::Result> Result;  
  95. };  
  96. //移除重复元数  
  97. template<class T>struct NoDuplicates;  
  98. template<>  
  99. struct NoDuplicates<NULLType>{  
  100.     typedef NULLType Result;  
  101. };  
  102. template<typename T, typename N>  
  103. struct NoDuplicates<typelist<T,N> >{  
  104. private:  
  105.     typedef typename NoDuplicates<N>::Result L1;  
  106.     typedef typename Erase<L1, T>::Result L2;  
  107.     //用EraseAll也不会错的。  
  108. public:  
  109.     typedef typelist<T,L2> Result;  
  110. };  
  111. //取代typelist中的某个元素  
  112. template<typename T, typename U, typename V>  
  113. struct Replace;  
  114. template<typename T, typename U>  
  115. struct Replace<NULLType, T, U>{  
  116.     typedef NULLType Result;  
  117. };  
  118. template<typename T, typename U, typename V>  
  119. struct Replace<typelist<T,U>,T, V>{  
  120.     typedef typelist<V,U> Result;  
  121. };  
  122. template<typename T, typename U, typename V,typename W>  
  123. struct Replace<typelist<T,U>, V ,W>{  
  124.     typedef typelist<T,typename Replace<U, V, W>::Result > Result;  
  125. };  
  126. int _tmain(int argc, _TCHAR* argv[])  
  127. {     
  128.     using namespace std;  
  129.     //typelist的结束应该是以NULLType结束,否则会错。因为IndexOf是假定这种情况存在。  
  130.     typedef Append<typelist<char,typelist<int,NULLType>>, string>::Result ret;  
  131.     cout<<IndexOf<ret,int>::index<<endl;  
  132.     //std::cout<<typeid(s).name()<<std::endl;  
  133.     //Loki::SmartPtr<Text>p(new Text);  
  134.     //p->output();  
  135.     //std::cout<<length<typelist1>::len<<std::endl;  
  136.     //TypeAt<typelist1,3>::type m;  
  137.     //std::cout<<typeid(m).name()<<std::endl;  
  138.     //std::cout<<IndexOf<typelist1,int>::index<<std::endl;  
  139.     return 0;  
  140. }  

看了这麽多,主要是三点:递归: 递归的步长,递归的结束条件; 嵌套; 模板的特化与偏特化;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值