Typelist 笔记

#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yshuise

权术横行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值