TypeList 之 Replace,把类型列表中的 T 类型换成 U 类型
来源:互联网 发布:crystal ball软件 编辑:程序博客网 时间:2024/06/05 23:51
#include <cstdlib>
#include <iostream>
using namespace std;
class NullType;
namespace TL
{
template<class T,class U>
struct TypeList
{
typedef T head;
typedef U tail;
};
#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)>
//-----------------------------------------------------------------
//Length
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;
};
//---------------------------------------------------------------
//TypeList 之 Replace,把类型列表中的 T 类型换成 U 类型
template<class TList,class T,class U>struct Replace;
template<class T,class U>
struct Replace<NullType,T,U>
{
typedef NullType Result;
};
template<class T,class U,class tail>
struct Replace<TypeList<T,tail>,T,U>
{
typedef TypeList<U,tail> Result;
};
template<class head,class tail,class T,class U>
struct Replace<TypeList<head,tail>,T,U>
{
typedef TypeList<head,typename Replace<tail,T,U>::Result> Result;
};
}
using namespace TL;
int main(int argc, char *argv[])
{
typedef TYPELIST_4(int,float,char,double) Test1;
Test1::head n = 1011;
cout<<"替换类型之前是整数类型,其值 = "<<n<<endl;
typedef Replace<Test1,int,unsigned char>::Result TestRes;
TestRes::head cc[] = "现在变成 char 了";
cout<<"替换类型之后是char类型,其值 = "<<cc<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
- TypeList 之 Replace,把类型列表中的 T 类型换成 U 类型
- TypeList 之 DerivedToFront 把类型链表中类型按继承层次排序(子类 -> 基类)
- TypeList 之TypeAt 通过索引查找TypeList的类型
- IEnumerable<T>类型直接转换成DataTable
- redis之列表类型
- T-SQL中的整数类型
- 【c#】把Dataset类型转为List<T>
- Redis之List 列表类型
- WSS3SDK之:向列表添加内容类型中的栏
- string类型的Replace函数
- Python语言中的列表类型--Python(14)
- 《python中的集中列表类型总结》
- 类型
- 类型
- 类型
- 类型
- 类型
- 类型
- rsync的一个巨傻逼无比的错误!
- Web Part Page Maintenance(Redirect to this page)
- 研究生导师肺腑之言
- Java操作图片改变大小加水印
- 字符串类型ip同数值类型ip转换
- TypeList 之 Replace,把类型列表中的 T 类型换成 U 类型
- 利用php调用C语言 扩展函数
- 在Wince上造背景透明的控件
- 新浪围脖测试
- Programming Contest of Zhengzhou Univercity(Problems Guidence)
- Android系统搜索对话框设计妙招 - 转帖
- netif_start_queue函数
- ASP.NET模板控件概念
- 六步绘制企业战略地图