65,187
社区成员




下面的代码要是读过<<Modern C++ Design>>兴许能看懂,但能想到这种设计就
绝对是功力的问题。
#include <iostream>
using namespace std;
struct NullType;
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(T1,T2,T3)>
template <class Tlist> struct MaxType;
template <>
struct MaxType<NullType> //全特化,空类型时令Max为-1
{
enum{Max=-1};
};
template <class Head,class Tail>
struct MaxType<typelist<Head,Tail> > //偏特化
{
enum {size=sizeof(Head)};
enum { Max=( size > MaxType<Tail>::Max ) ? size : MaxType<Tail>::Max }; //前面是size
//而比较的后面又是Max,谁能讲讲?
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef typelist_4(char,float,double,char) types_4;
cout<<MaxType<types_4>::Max<<endl; //求得double类型sizeof值是最大的为8
return 0;
}
能谈谈各自的看法和理解,再此谢谢了! (附:在vs2005中调试通过)
感谢yshuise 的回复,如你所说这个东西是基础
同时恭喜你“我已经跨过这个坎了”,我正在迈!...
求类型的最大字节数吧.
typedef typelist_4(char,float,double,char) types_4;
把这个条件改为小一些这样推导更方便:
typedef typelist<char, typelist<double, typelist<float, NULLType> > > type;
template <class Head,class Tail>
struct MaxType<typelist<Head,Tail> > //偏特化
{
enum {size=sizeof(Head)};
enum { Max=( size > MaxType<Tail>::Max ) ? size : MaxType<Tail>::Max }; //前面是size
//而比较的后面又是Max,谁能讲讲?
};
首先:
struct MaxType<typelist<char, typelist<double, typelist<float, NULLType> > > >
{
//Head = char;
//Tail = typelist<double, typelist<float, NULLType> > >;
enum {size = sizeof(char)}; //size = 1;
enum {Max = (1 > MaxType<typelist<double, typelist<float, NULLType> >>::Max)? 1 : MaxType<typelist<double, typelist<float, NULLType> >>::Max
};
因为:MaxType<typelist<double, typelist<float, NULLType> >>::Max的值不知道,所以要继续特化(递归):
struct MaxType<typelist<double, typelist<float, NULLType> >>
{
//Head = double;
//Tail = typelist<float, NULLType>;
enum{size = sizeof(double)} //size = 8
enum{Max = (8 > MaxType<typelist<float, NULLType>>::Max)? 8: MaxType<typelist<float, NULLType>>::Max
};
还要求:MaxType<typelist<float, NULLType>>::Max
struct MaxType<typelist<float, NULLType>>{
//Head = float;
//Tail = NULLType;
enum{size = sizeof(float)};
enum {Max = size > MaxTpe<NULLType>::Max ?.....}//这儿再也不需要特化了,
};
template <class Head,class Tail>
struct MaxType<typelist<Head,Tail> > //偏特化
{
enum {size=sizeof(Head)};
enum { Max=( size > MaxType<Tail>::Max ) ? size : MaxType<Tail>::Max }; //前面是size
//而比较的后面又是Max,谁能讲讲?
};
纠正一个错误:
#define typelist_4(T1,T2,T3,T4) typelist<T1,typelist_3(T1,T2,T3)>
改为:
#define typelist_4(T1,T2,T3,T4) typelist<T1,typelist_3(T2,T3,T4)>
顺便建议没看过<<Modern C++ Design>>这本书的老鸟们,怎么也得抽时间看看,
感觉里面的一些思维方式犹如醍醐灌顶,给人一种全新的境界,template泛型的使用
也是到达了登峰造极的程度,能把c/c++的型别玩到随心所欲的地步,本人一直就认为
要成为c/c++程序员必须对c/c++的类型和内存数据操作理解到一定的高度,才能算作
进入门槛了
template <class Head,class Tail>
struct MaxType<typelist<Head,Tail> > //偏特化
{
private:
enum { size1=MaxType<Tail>::Max};
public:
enum {size=sizeof(Head)};
enum { Max=( size > size1 ) ? size : size1 };
};