產生模板的自動機制

來源:互聯網
上載者:User

#include "stdafx.h"<br />#include <iostream><br />#include <string><br />#include <typeinfo><br />#include <boost/mpl/vector.hpp><br />#include <boost/mpl/push_back.hpp><br />#include <boost/mpl/begin_end.hpp><br />#include <boost/mpl/at.hpp><br />#include <boost/mpl/erase.hpp><br />#include <boost/mpl/pop_front.hpp><br />#include <boost/mpl/int.hpp><br />#include <boost/mpl/deref.hpp><br />#include <boost/mpl/front.hpp><br />#include <boost/mpl/back.hpp><br />using std::type_info;<br />using std::string;<br />using std::cout;<br />using std::endl;<br />using boost::mpl::at ;<br />using boost::mpl::int_;<br />using boost::mpl::pop_front;<br />using boost::mpl::vector;<br />using boost::mpl::deref;<br />using boost::mpl::begin;<br />using boost::mpl::front;<br />using boost::mpl::back;</p><p>struct NullType{ };<br />template<int N><br />struct Int2Type<br />{<br />enum{ vaule = N };<br />};<br />template <class T><br />struct Holder{<br />typename T vaule;<br />};<br />//允許模板參數的最大值為10。 也可以自己嵌套寫,用vector, 這樣可以寫更多的參數。<br />template <typename T1, typename T2 = NullType, typename T3 = NullType,<br />typename T4= NullType,typename T5 = NullType, typename T6 = NullType,<br />typename T7 = NullType,typename T8= NullType,typename T9 = NullType,<br />typename T10 = NullType><br />struct my_vector{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6,<br />vector<T7, vector<T8,vector<T9,vector<T10,NullType>>>>>>>>>> type;<br />};<br />template <typename T1><br />struct my_vector<T1>{<br />typedef vector<T1,NullType> type;<br />};<br />template <typename T1, typename T2><br />struct my_vector<T1,T2>{<br />typedef vector<T1,vector<T2,NullType>> type;<br />};<br />template <typename T1, typename T2, typename T3><br />struct my_vector<T1,T2,T3>{<br />typedef vector<T1,vector<T2,vector<T3,NullType>>> type;<br />};<br />template <typename T1, typename T2, typename T3, typename T4><br />struct my_vector<T1,T2,T3,T4>{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,NullType>>>> type;<br />};<br />template <typename T1, typename T2, typename T3, typename T4, typename T5><br />struct my_vector<T1,T2,T3,T4,T5>{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,<br />vector<T5,NullType>>>>> type;<br />};<br />template <typename T1, typename T2, typename T3, typename T4,<br />typename T5,typename T6><br />struct my_vector<T1,T2,T3,T4,T5,T6>{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,<br />vector<T6, NullType>>>>>> type;<br />};<br />template <typename T1, typename T2, typename T3, typename T4,<br />typename T5,typename T6,typename T7><br />struct my_vector<T1,T2,T3,T4,T5,T6,T7>{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6,<br />vector<T7, NullType>>>>>>> type;<br />};<br />template <typename T1, typename T2, typename T3, typename T4, typename T5,<br />typename T6,typename T7,typename T8><br />struct my_vector<T1,T2,T3,T4,T5,T6,T7,T8>{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6, vector<T7,<br />vector<T8,NullType>>>>>>>> type;<br />};<br />template <typename T1, typename T2, typename T3, typename T4,<br />typename T5,typename T6,typename T7,typename T8,typename T9><br />struct my_vector<T1,T2,T3,T4,T5,T6,T7,T8,T9>{<br />typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6,<br />vector<T7, vector<T8,vector<T9,NullType>>>>>>>>> type;<br />};<br />template<class TList, unsigned int index> struct TypeAt;<br />template <class Head, class Tail><br />struct TypeAt<vector<Head, Tail>,0><br />{<br />typedef Head Result;<br />};<br />template <class Head, class Tail, unsigned int i><br />struct TypeAt<vector<Head, Tail>, i><br />{<br />typedef typename TypeAt<Tail, i-1>::Result Result;<br />};<br />template <class TList, template<class> class Unit><br />class GenScatterHierarchy;//主模板<br />template<class T1, class T2, template<class> class Unit><br />class GenScatterHierarchy<vector<T1,T2> , Unit>//對主模進行特化。<br />: public GenScatterHierarchy<T1, Unit><br />, public GenScatterHierarchy<T2, Unit><br />{<br />};<br />template<class ATomicType, template<class> class Unit><br />class GenScatterHierarchy : public Unit<ATomicType><br />{<br />};<br />template<template<class> class Unit><br />class GenScatterHierarchy<NullType, Unit>{<br />};</p><p>template<class TList, template<class> class Unit><br />Unit<typename front<TList>::type >& FieldHelper(<br />GenScatterHierarchy<TList,Unit>& obj, Int2Type<0> )<br />{<br />GenScatterHierarchy<typename front<TList>::type, Unit>& leftBase = obj;<br />return leftBase;<br />};<br />template<int i, class TList, template<class> class Unit><br />Unit< typename TypeAt<TList,i>::Result >& FieldHelper(<br />GenScatterHierarchy<TList, Unit>& obj, Int2Type<i>)<br />{<br />typename GenScatterHierarchy<typename at<TList,int_<1>>::type, Unit>& rightBase = obj;<br />return FieldHelper(rightBase, Int2Type<i-1>());<br />}<br />template <int i, class TList, template<class> class Unit><br />Unit<typename TypeAt<TList,i>::Result >&<br />Field (GenScatterHierarchy<TList, Unit>& obj)<br />{<br />return FieldHelper(obj, Int2Type<i>() );<br />}</p><p>int _tmain(int argc, _TCHAR* argv[])<br />{<br />typedef GenScatterHierarchy<typename my_vector<int,float,string>::type, Holder> Info;<br />Info obj;<br />float b = (static_cast<Holder<float>&>(obj)).vaule;//ok<br />//deref<begin<vector<int>::type>::type>::type a=9;<br />int a = Field<0>(obj).vaule;<br />float fl = Field<1>(obj).vaule;<br />string str = Field<2>(obj).vaule;</p><p>return 0;<br />}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.