TypeList 之 NoDuplicate
来源:互联网 发布:神优化的单机游戏 编辑:程序博客网 时间:2024/05/21 17:20
#include <cstdlib>
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
class NullType;
namespace TL
{
template<class T,class U>
struct TypeList
{
typedef T head;
typedef U tail;
};
}
using namespace TL;
#define TYPELIST_1(T1) TypeList<T1,NullType>
#define TYPELIST_2(T1,T2) TypeList<T1,TYPELIST_1(T2)>
#define TYPELIST_3(T1,T2,T3) TypeList<T1,TYPELIST_2(T2,T3)>
#define TYPELIST_4(T1,T2,T3,T4) TypeList<T1,TYPELIST_3(T2,T3,T4)>
typedef TypeList<char,TypeList<signed char,unsigned char> > CharList;
template<class Tlist>struct Length;
template<>struct Length<NullType>
{
enum{value = 0};
};
template<class T,class U>
struct Length<TypeList<T,U> >
{
enum{value = 1 + Length<U>::value};
};
//---------------------------------------------------------------
//利用索引查找对象
template<class T,int U>struct TypeAt;
template<class head,class tail>
struct TypeAt<TypeList<head,tail>,0>
{
typedef head Result;
};
template<class head,class tail,int i>
struct TypeAt<TypeList<head,tail>,i>
{
typedef typename TypeAt<tail,i - 1>::Result Result;
};
//---------------------------------------------------------------
//indexof
template<class TList,class T>struct IndexOf;
template<class T>
struct IndexOf<NullType,T>
{
enum{value = -1};
};
template<class Tail,class T>
struct IndexOf<TypeList<T,Tail>,T>
{
enum{value = 0};
};
template<class Head,class Tail,class T>
struct IndexOf<TypeList<Head,Tail>,T>
{
private:
enum{temp = IndexOf<Tail,T>::value};
public:
enum{value = temp == -1 ? -1 : 1 + temp};
};
//---------------------------------------------------------------
//Append
template<class TList,class T>struct Append;
template<>
struct Append<NullType,NullType>
{
typedef NullType Result;
};
template<class T>
struct Append<NullType,T>
{
typedef TYPELIST_1(T) Result;
};
template<class head,class Tail>
struct Append<NullType,TypeList<head,Tail> >
{
typedef TypeList<head,Tail> Result;
};
template<class head,class Tail,class T>
struct Append<TypeList<head,Tail>,T>
{
typedef TypeList<head,typename Append<Tail,T>::Result> Result;
};
//---------------------------------------------------------------
//Erase
template<class TList,class T>struct Erase;
template<class T>
struct Erase<NullType,T>
{
typedef NullType Result;
};
template<class T,class tail>
struct Erase<TypeList<T,tail>,T>
{
typedef tail Result;
};
template<class head,class tail,class T>
struct Erase<TypeList<head,tail>,T>
{
typedef TypeList<head,typename Erase<tail,T>::Result> Result;
};
//---------------------------------------------------------------
//NoDuplicate
template<class TList>struct NoDuplicate;
template<>struct NoDuplicate<NullType>
{
typedef NullType Result;
};
template<class head,class tail>
struct NoDuplicate<TypeList<head,tail> >
{
private:
typedef typename NoDuplicate<tail>::Result L1;
typedef typename Erase<L1,head>::Result L2;
public:
typedef TypeList<head,L2> Result;
};
int main(int argc, char *argv[])
{
cout<<"NoDuplicate-----------------------------------------"<<endl;
typedef TYPELIST_4(char,signed char,char,signed char) CharList3;
cout<<"CharList3去掉重复之前的长度:"<<Length<CharList3>::value<<endl;
typedef NoDuplicate<CharList3>::Result CharList4;
cout<<"CharList4去掉重复之后的长度:"<<Length<CharList4>::value<<endl;
cout<<"-----------------------------------------"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
- TypeList 之 NoDuplicate
- TypeList 之 Append
- TypeList 之 Erase
- TypeList 之应用 GenScatterHierarchy
- TypeList
- TypeList 之TypeAt 通过索引查找TypeList的类型
- Loki源码阅读之TypeList(未完)
- 模板之Typelist的"增删改查"
- 递归之美 - Loki库TypeList源码剖析
- 递归之美 - Loki库TypeList源码剖析
- 递归之美 - Loki库TypeList源码剖析
- Typelist 笔记
- TypeList IndexOf
- typelist 学习
- Typelist 笔记
- Typelist介绍
- 扩展TypeList:Typelist生成器、MaxSizeOf
- 关于模板偏特化,模板递归,及Loki之Typelist和一些感悟
- 『转』Android 源码分析
- URLDecode的最初级使用
- 学习PHP中的XML解析
- POJ 1141 Brackets Sequence 动态规划
- linux的常用命令
- TypeList 之 NoDuplicate
- 《Windows核心编程》读书心得——字符和字符串处理(1)
- Chapter3 Windows and Messages —— Programming Windows 笔记
- [NPWP笔记]perl创建进程
- 制作Java Applet签名验证
- MFC 中 WM_NOTIFY messages 传递过程
- 转发屠夫大牛使用未公开API ZwQueryVirtualMemory 枚举进程模块的函数例子
- 流水灯&万用板
- vs2005 无法新建 c++ Smart Device的工程解决方案