模板的模板类

Loki里面有个基于TypeList的GenScatterHierarchy的模板类用来生成杂乱的继承关系,tuple的核心就是这么实现的。看了这个之后,发现Loki真的是太强大了,所以直接写了一个模板的模板类的demo,用以学习。见如下demo代码

#include <cassert>
#include <iostream>
using namespace std;

template<class , template<class> class >
struct GenTempl;

template<class T, template<class> class TT >
struct GenTempl
{
	typedef typename TT<T> TemplateType;
};

template<template<class> class TT>
struct GenTempl<void, TT>
{
	typedef void TemplateType;
};

template<class> class TemplClass;
class BasicClass
{
public:
	virtual ~BasicClass(){};
	int m_basevalue;
};

template<class T>
class TemplClass
{
public:
	
	T m_class;
};

template<class T, class U>
struct IsSame
{
	static enum {value = false,};
};

template<class T>
struct IsSame<T,T>
{
	static enum {value =  true,};
};

class DerivedClass :public BasicClass
{
public:
	virtual ~DerivedClass(){}
	int m_derivedvalue;
};

void test_templ_class()
{
	typedef GenTempl<BasicClass, TemplClass>::TemplateType HolderClass;
	typedef GenTempl<void, TemplClass>::TemplateType VoidType;

	assert((IsSame<VoidType, void>::value));
	assert((IsSame<HolderClass, TemplClass<BasicClass>>::value));

	HolderClass hc;
	hc.m_class.m_basevalue = 1000;

	DerivedClass d;
	BasicClass & b = dynamic_cast<BasicClass&>(d);

	int stop_pt = 0;
};

int _tmain(int argc, _TCHAR* argv[])
{
	test_templ_class();

	std::cout<<"test all passed!!!"<<std::endl;

	getchar();
	return 0;
}


你可能感兴趣的:(loki)