以前看Typelist还没有真的理解,现在算理解了。
不过觉得Loki技巧过于华丽,也就是语言技巧。有些不实用,还是Boost更实用。
今天转移到boost库去了,这是昨天的笔记。
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <typeinfo>
- //#include <loki/SmartPtr.h>
- // class Text{
- // public:
- // void output(){
- // cout<<"Hello,world!"<<endl;
- // }
- // };
- //“看在上帝的份上,从简单开始吧”——《具体数学》
- template <typename T, typename N>
- struct typelist{
- typedef T head;
- typedef N tail;
- };
- struct NULLType;
- typedef typelist<char,typelist<char,typelist<float,typelist<int, NULLType> > > > typelist1;
- //计算长度
- template<typename typelist>
- struct length;
- template<>
- struct length<NULLType>{
- enum{ len = 0};
- };
- //假如以typelist<char, typelist<string, NULLType>> 为例
- //
- template<typename T, typename N>
- struct length< typelist<T,N> >{//这儿的N就是typelist<string,NULLType>
- enum{len = 1+ length<N>::len};//递归调用上面,直到遇到一个特化版本
- };
- //索引访问,下标是从0开始。
- //这儿是模板参数的声明,决定模板参数的个数
- //也就是后来的typelist<...>的参数个数
- template<typename typelist, int i>//这儿表明有两个模板参数
- struct TypeAt;
- //这儿的i是从最左边的开始数起
- template<typename T, typename N,int i>
- struct TypeAt<typelist<T,N>,i>{//参数格式是2,分别是typelist<T,N>、i
- typedef typename TypeAt<N,i-1>::type type;
- };
- template<typename T, typename N>
- struct TypeAt<typelist<T,N>, 0 >{
- typedef T type;
- };
- //查找typelist
- //只考虑了正确的情况,没有容错功能
- template<typename T, typename N>
- struct IndexOf;
- template<typename T, typename N>
- struct IndexOf<typelist<T,N>, T>{
- enum{index = 0};
- };
- template<typename T, typename N, typename V> //
- struct IndexOf<typelist<T,N>, V>{
- enum {index = 1 + IndexOf<N,V>::index};
- };
- //附加元素到typelist
- template<typename T, typename N>
- struct Append;
- template<typename T, typename N, typename V>
- struct Append<typelist<T,N>, V>{
- typedef typelist<V, typelist<T,N> > Result;
- };
- //erase
- template<typename T, typename N>
- struct Erase;
- template<typename T, typename N>
- struct Erase<typelist<T,N>,T>{
- typedef N Result;
- };
- template<typename T, typename N, typename C>
- struct Erase<typelist<T,N>,C>{
- private:
- typedef typename Erase<N, C>::Result Ret;
- public:
- typedef typelist<T,Ret> Result;
- };
- //eraseall 查处全部相同的元素。
- template<typename T, typename N>
- struct EraseAll;
- template<typename T>
- struct EraseAll<NULLType, T>{
- typedef NULLType Result;
- };
- template<typename T, typename N>
- struct EraseAll<typelist<T, N>,T>{
- typedef N Result;
- };
- template<typename T, typename N, typename V>
- struct EraseAll<typelist<T, N>, V>{
- typedef typelist<T, typename EraseAll<N,V>::Result> Result;
- };
- //移除重复元数
- template<class T>struct NoDuplicates;
- template<>
- struct NoDuplicates<NULLType>{
- typedef NULLType Result;
- };
- template<typename T, typename N>
- struct NoDuplicates<typelist<T,N> >{
- private:
- typedef typename NoDuplicates<N>::Result L1;
- typedef typename Erase<L1, T>::Result L2;
- //用EraseAll也不会错的。
- public:
- typedef typelist<T,L2> Result;
- };
- //取代typelist中的某个元素
- template<typename T, typename U, typename V>
- struct Replace;
- template<typename T, typename U>
- struct Replace<NULLType, T, U>{
- typedef NULLType Result;
- };
- template<typename T, typename U, typename V>
- struct Replace<typelist<T,U>,T, V>{
- typedef typelist<V,U> Result;
- };
- template<typename T, typename U, typename V,typename W>
- struct Replace<typelist<T,U>, V ,W>{
- typedef typelist<T,typename Replace<U, V, W>::Result > Result;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- using namespace std;
- //typelist的结束应该是以NULLType结束,否则会错。因为IndexOf是假定这种情况存在。
- typedef Append<typelist<char,typelist<int,NULLType>>, string>::Result ret;
- cout<<IndexOf<ret,int>::index<<endl;
- //std::cout<<typeid(s).name()<<std::endl;
- //Loki::SmartPtr<Text>p(new Text);
- //p->output();
- //std::cout<<length<typelist1>::len<<std::endl;
- //TypeAt<typelist1,3>::type m;
- //std::cout<<typeid(m).name()<<std::endl;
- //std::cout<<IndexOf<typelist1,int>::index<<std::endl;
- return 0;
- }
看了这麽多,主要是三点:递归: 递归的步长,递归的结束条件; 嵌套; 模板的特化与偏特化;