产生模板的自动机制

#include "stdafx.h"
#include <iostream>
#include <string>
#include <typeinfo>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/erase.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/back.hpp>
using std::type_info;
using std::string;
using std::cout;
using std::endl;
using boost::mpl::at ;
using boost::mpl::int_;
using boost::mpl::pop_front;
using boost::mpl::vector;
using boost::mpl::deref;
using boost::mpl::begin;
using boost::mpl::front;
using boost::mpl::back;

struct NullType{ };
template<int N>
struct Int2Type 
{
	enum{ vaule = N };
};
template <class T>
struct Holder{
	typename T vaule;
};
//允许模板参数的最大值为10。 也可以自己嵌套写,用vector, 这样可以写更多的参数。
template <typename T1, typename T2 = NullType, typename T3 = NullType,
typename T4= NullType,typename T5 = NullType, typename T6 = NullType, 
typename T7 = NullType,typename T8= NullType,typename T9 = NullType, 
typename T10 = NullType>
struct my_vector{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6, 
		vector<T7, vector<T8,vector<T9,vector<T10,NullType>>>>>>>>>> type;
};
template <typename T1>
struct my_vector<T1>{
	typedef vector<T1,NullType> type;
};
template <typename T1, typename T2>
struct my_vector<T1,T2>{
	typedef vector<T1,vector<T2,NullType>> type;
};
template <typename T1, typename T2, typename T3>
struct my_vector<T1,T2,T3>{
	typedef vector<T1,vector<T2,vector<T3,NullType>>> type;
};
template <typename T1, typename T2, typename T3, typename T4>
struct my_vector<T1,T2,T3,T4>{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,NullType>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, typename T5>
struct my_vector<T1,T2,T3,T4,T5>{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,
		vector<T5,NullType>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, 
typename T5,typename T6>
struct my_vector<T1,T2,T3,T4,T5,T6>{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,
		vector<T6, NullType>>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, 
typename T5,typename T6,typename T7>
struct my_vector<T1,T2,T3,T4,T5,T6,T7>{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6, 
		vector<T7, NullType>>>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6,typename T7,typename T8>
struct my_vector<T1,T2,T3,T4,T5,T6,T7,T8>{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6, vector<T7,
		vector<T8,NullType>>>>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, 
typename T5,typename T6,typename T7,typename T8,typename T9>
struct my_vector<T1,T2,T3,T4,T5,T6,T7,T8,T9>{
	typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6, 
		vector<T7, vector<T8,vector<T9,NullType>>>>>>>>> type;
};
template<class TList, unsigned int index> struct TypeAt;
template <class Head, class Tail>
struct TypeAt<vector<Head, Tail>,0>
{
	typedef Head Result;
};
template <class Head, class Tail, unsigned int i>
struct TypeAt<vector<Head, Tail>, i>
{
	typedef typename TypeAt<Tail, i-1>::Result Result;
};
template <class TList, template<class> class Unit>
class GenScatterHierarchy;//主模板
template<class T1, class T2, template<class> class Unit>
class GenScatterHierarchy<vector<T1,T2> , Unit>//对主模进行特化。
	: public GenScatterHierarchy<T1, Unit>
	, public GenScatterHierarchy<T2, Unit>
{
};
template<class ATomicType, template<class> class Unit>
class GenScatterHierarchy : public Unit<ATomicType>
{
};
template<template<class> class Unit>
class GenScatterHierarchy<NullType, Unit>{
};

template<class TList, template<class> class Unit>
Unit<typename front<TList>::type >& FieldHelper(
	GenScatterHierarchy<TList,Unit>& obj, Int2Type<0> )
{
	GenScatterHierarchy<typename front<TList>::type, Unit>& leftBase = obj;
	return leftBase;
};
template<int i, class TList, template<class> class Unit>
Unit< typename TypeAt<TList,i>::Result >& FieldHelper(
	GenScatterHierarchy<TList, Unit>& obj, Int2Type<i>)
{
	typename GenScatterHierarchy<typename at<TList,int_<1>>::type, Unit>& rightBase = obj;
	return FieldHelper(rightBase, Int2Type<i-1>());
}
template <int i, class TList, template<class> class Unit>
Unit<typename TypeAt<TList,i>::Result >&
Field (GenScatterHierarchy<TList, Unit>& obj)
{
	return FieldHelper(obj, Int2Type<i>() );
}

int _tmain(int argc, _TCHAR* argv[])
{  
	typedef GenScatterHierarchy<typename my_vector<int,float,string>::type, Holder> Info;
	Info obj;
	float b = (static_cast<Holder<float>&>(obj)).vaule;//ok
	//deref<begin<vector<int>::type>::type>::type  a=9;
	int a = Field<0>(obj).vaule;
	float fl =  Field<1>(obj).vaule;
	string str = Field<2>(obj).vaule;

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yshuise

权术横行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值