#include <cstdlib>
#include <iostream>
#include <typeinfo>
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)>
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};
};
int main(int argc, char *argv[])
{
typedef TYPELIST_3(char,signed char,unsigned char) MyChar;
cout<<"模版链表长度-----------------------------------------"<<endl;
cout<<"MyChar 长度:"<<Length<MyChar>::value<<endl;
type_info* intsRtti[Length<MyChar>::value];
system("PAUSE");
return EXIT_SUCCESS;
}
TypeList 的长度计算 Length
最新推荐文章于 2025-08-26 14:36:45 发布
本文介绍了一个使用C++模板元编程实现的链表长度计算方法。通过宏定义创建不同类型的链表,并利用特化模板来计算这些链表的长度。演示了如何定义含有三种类型(char, signed char, unsigned char)的链表并输出其长度。
1078

被折叠的 条评论
为什么被折叠?



