• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Cpper
            C/C++高級(jí)工程師 Android高級(jí)軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿
            接上文
            下面看看TypeList的類型刪除功能
            相關(guān)源碼為:

            ////////////////////////////////////////////////////////////////////////////////
            // class template Erase
            // Erases the first occurence, if any, of a type in a typelist
            // Invocation (TList is a typelist and T is a type):
            // Erase<TList, T>::Result
            // returns a typelist that is TList without the first occurence of T
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T> struct Erase;
                    
                    template 
            <class T>                         // Specialization 1
                    struct Erase<NullType, T>
                    
            {
                        typedef NullType Result;
                    }
            ;

                    template 
            <class T, class Tail>             // Specialization 2
                    struct Erase<Typelist<T, Tail>, T>
                    
            {
                        typedef Tail Result;
                    }
            ;

                    template 
            <class Head, class Tail, class T> // Specialization 3
                    struct Erase<Typelist<Head, Tail>, T>
                    
            {
                        typedef Typelist
            <Head, 
                                typename Erase
            <Tail, T>::Result>
                            Result;
                    }
            ;

            正如所說
            Erase刪除的類型是第一次出現(xiàn)的類型
            其特化版本有3個(gè)第一個(gè)是針對(duì)空類型鏈表的刪除
            其二和其三的相互迭代構(gòu)成了對(duì)所有位置類型的刪除動(dòng)作

            TypeList的下一個(gè)操作是對(duì)鏈表的給定類型清空動(dòng)作
            ////////////////////////////////////////////////////////////////////////////////
            // class template EraseAll
            // Erases all first occurences, if any, of a type in a typelist
            // Invocation (TList is a typelist and T is a type):
            // EraseAll<TList, T>::Result
            // returns a typelist that is TList without any occurence of T
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T> struct EraseAll;
                    template 
            <class T>
                    
            struct EraseAll<NullType, T>
                    {
                        typedef NullType Result;
                    };
                    template 
            <class T, class Tail>
                    
            struct EraseAll<Typelist<T, Tail>, T>
                    {
                        
            // Go all the way down the list removing the type
                        typedef typename EraseAll<Tail, T>::Result Result;
                    };
                    template 
            <class Head, class Tail, class T>
                    
            struct EraseAll<Typelist<Head, Tail>, T>
                    {
                        
            // Go all the way down the list removing the type
                        typedef Typelist<Head, 
                                typename EraseAll
            <Tail, T>::Result>
                            Result;
                    };

            接著,下面的代碼實(shí)現(xiàn)的功能分別是
            1.刪除類型鏈表中所有重復(fù)的類型
            2.類型替換的解決

            1.刪除類型鏈表中重復(fù)的類型
            代碼為:
            ////////////////////////////////////////////////////////////////////////////////
            // class template NoDuplicates
            // Removes all duplicate types in a typelist
            // Invocation (TList is a typelist):
            // NoDuplicates<TList, T>::Result
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList> struct NoDuplicates;
                    
                    template 
            <> struct NoDuplicates<NullType>
                    {
                        typedef NullType Result;
                    };

                    template 
            <class Head, class Tail>
                    
            struct NoDuplicates< Typelist<Head, Tail> >
                    {
                    
            private:
                        typedef typename NoDuplicates
            <Tail>::Result L1;
                        typedef typename Erase
            <L1, Head>::Result L2;
                    
            public:
                        typedef Typelist
            <Head, L2> Result;
                    };
            如果鏈表為空則不操作
            如果鏈表為非空則進(jìn)行以下動(dòng)作
            刪除位置位置為1,2,3,..的重復(fù)類型

            下面的功能則是類型替換和鏈表翻轉(zhuǎn)
            ////////////////////////////////////////////////////////////////////////////////
            // class template Replace
            // Replaces the first occurence of a type in a typelist, with another type
            // Invocation (TList is a typelist, T, U are types):
            // Replace<TList, T, U>::Result
            // returns a typelist in which the first occurence of T is replaced with U
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T, class U> struct Replace;
                    
                    template 
            <class T, class U>
                    
            struct Replace<NullType, T, U>
                    {
                        typedef NullType Result;
                    };

                    template 
            <class T, class Tail, class U>
                    
            struct Replace<Typelist<T, Tail>, T, U>
                    {
                        typedef Typelist
            <U, Tail> Result;
                    };

                    template 
            <class Head, class Tail, class T, class U>
                    
            struct Replace<Typelist<Head, Tail>, T, U>
                    {
                        typedef Typelist
            <Head,
                                typename Replace
            <Tail, T, U>::Result>
                            Result;
                    };

            ////////////////////////////////////////////////////////////////////////////////
            // class template ReplaceAll
            // Replaces all occurences of a type in a typelist, with another type
            // Invocation (TList is a typelist, T, U are types):
            // Replace<TList, T, U>::Result
            // returns a typelist in which all occurences of T is replaced with U
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T, class U> struct ReplaceAll;
                    
                    template 
            <class T, class U>
                    
            struct ReplaceAll<NullType, T, U>
                    {
                        typedef NullType Result;
                    };
                    
                    template 
            <class T, class Tail, class U>
                    
            struct ReplaceAll<Typelist<T, Tail>, T, U>
                    {
                        typedef Typelist
            <U, typename ReplaceAll<Tail, T, U>::Result> Result;
                    };
                    
                    template 
            <class Head, class Tail, class T, class U>
                    
            struct ReplaceAll<Typelist<Head, Tail>, T, U>
                    {
                        typedef Typelist
            <Head,
                                typename ReplaceAll
            <Tail, T, U>::Result>
                            Result;
                    };

            ////////////////////////////////////////////////////////////////////////////////
            // class template Reverse
            // Reverses a typelist
            // Invocation (TList is a typelist):
            // Reverse<TList>::Result
            // returns a typelist that is TList reversed
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList> struct Reverse;
                    
                    template 
            <>
                    
            struct Reverse<NullType>
                    {
                        typedef NullType Result;
                    };
                    
                    template 
            <class Head, class Tail>
                    
            struct Reverse< Typelist<Head, Tail> >
                    {
                        typedef typename Append
            <
                            typename Reverse
            <Tail>::Result, Head>::Result Result;
                    };

            類型鏈表的最后一個(gè)設(shè)計(jì)的功能是鑒別類型鏈表中有多少個(gè)類型繼承于給定類型
            ////////////////////////////////////////////////////////////////////////////////
            // class template MostDerived
            // Finds the type in a typelist that is the most derived from a given type
            // Invocation (TList is a typelist, T is a type):
            // MostDerived<TList, T>::Result
            // returns the type in TList that's the most derived from T
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList, class T> struct MostDerived;
                    
                    template 
            <class T>
                    
            struct MostDerived<NullType, T>
                    {
                        typedef T Result;
                    };
                    
                    template 
            <class Head, class Tail, class T>
                    
            struct MostDerived<Typelist<Head, Tail>, T>
                    {
                    
            private:
                        typedef typename MostDerived
            <Tail, T>::Result Candidate;
                    
            public:
                        typedef typename Select
            <
                            SuperSubclass
            <Candidate,Head>::value,
                                Head, Candidate
            >::Result Result;
                    };

            ////////////////////////////////////////////////////////////////////////////////
            // class template DerivedToFront
            // Arranges the types in a typelist so that the most derived types appear first
            // Invocation (TList is a typelist):
            // DerivedToFront<TList>::Result
            // returns the reordered TList 
            ////////////////////////////////////////////////////////////////////////////////

                    template 
            <class TList> struct DerivedToFront;
                    
                    template 
            <>
                    
            struct DerivedToFront<NullType>
                    {
                        typedef NullType Result;
                    };
                    
                    template 
            <class Head, class Tail>
                    
            struct DerivedToFront< Typelist<Head, Tail> >
                    {
                    
            private:
                        typedef typename MostDerived
            <Tail, Head>::Result
                            TheMostDerived;
                        typedef typename Replace
            <Tail,
                            TheMostDerived, Head
            >::Result Temp;
                        typedef typename DerivedToFront
            <Temp>::Result L;
                    
            public:
                        typedef Typelist
            <TheMostDerived, L> Result;
                    };
            DerivedToFront是排列類型鏈表保持后面的類型繼承于前面的類型
            全文測(cè)試代碼如下:
            #include <iostream>
            #include 
            <string>
            #include 
            <Loki/TypeList.h>
            #include 
            <typeinfo>

            typedef 
            int Type;
            typedef Loki::TL::MakeTypelist
            <Type,
                                           
            char,
                                           
            long,
                                           
            bool,
                                           
            int,
                                           std::
            string,
                                           
            double,
                                           unsigned 
            int,
                                           
            long long,
                                           
            int> MyList;
            class Class{};
            class Class1: public Class{};
            class Class2: public Class{};                               
            class Class3: public Class{};

            int main()
            {
                MyList::Result hlist;
                std::cout 
            <<"MyList length "<<Loki::TL::Length<MyList::Result>::value<<std::endl; 
                Loki::TL::TypeAt
            <MyList::Result,1>::Result result;
                std::cout
            <<"the type in indexo of 1: "<<result<<std::endl; 
                Loki::TL::TypeAtNonStrict
            <MyList::Result,0>::Result _type; 
                std::cout
            <<"default value in index of 0:" <<_type<<std::endl;
                std::cout
            <<Loki::TL::IndexOf<MyList::Result,long>::value<<std::endl; 
                typedef Loki::TL::Append
            <MyList::Result,Class>::Result NewType;
                std::cout 
            <<"get length of NewType: "<< Loki::TL::Length<NewType>::value<<std::endl; 
                typedef Loki::TL::Erase
            <MyList::Result,double>::Result NewType2;
                std::cout
            <<"new length of NewType:"<<Loki::TL::Length<NewType2>::value<<std::endl; 
                typedef Loki::TL::EraseAll
            <MyList::Result,int>::Result NewType3;
                std::cout
            <<"new length of NewType after erase all int type:"<<Loki::TL::Length<NewType3>::value<<std::endl; 
                typedef Loki::TL::NoDuplicates
            <MyList::Result>::Result NewType4;
                std::cout
            <<"new length of New type after NoDuplicates "<<Loki::TL::Length<NewType4>::value<<std::endl;
                typedef Loki::TL::Replace
            <MyList::Result,int,Class>::Result NewType5;
                typedef Loki::TL::Append
            <MyList::Result,Class1>::Result NewType6;
                typedef Loki::TL::Append
            <MyList::Result,Class2>::Result NewType7;
                typedef Loki::TL::Append
            <MyList::Result,Class3>::Result NewType8;
                typedef Loki::TL::Append
            <MyList::Result,Class2>::Result NewType9;
                typedef Loki::TL::Append
            <MyList::Result,long>::Result NewType10;
                
                Loki::TL::MostDerived
            <NewType10,int>::Result r;
                std::cout
            <<typeid(Loki::TL::MostDerived<NewType10,Class>::Result).name()<<std::endl;
                Loki::TL::DerivedToFront
            <NewType10>::Result t; 
                std::cout
            <<"new length of New type after Arrange TypeList "<<Loki::TL::Length<NewType10>::value<<std::endl;
                
            int len = Loki::TL::Length<NewType10>::value;
                std::cout
            <<"the type name in given lindex : "<<typeid(Loki::TL::TypeAtNonStrict<MyList::Result,11>::Result).name()<<std::endl;
                system(
            "PAUSE");
                
            return EXIT_SUCCESS;
            }

            Loki TypeList寫完了
            感覺到了模板元編程的強(qiáng)大之處
            posted on 2010-04-17 10:23 ccsdu2009 閱讀(2001) 評(píng)論(4)  編輯 收藏 引用
            Comments

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


             
            要久久爱在线免费观看| 久久国产高清字幕中文| 国产AV影片久久久久久| 精品人妻伦一二三区久久| 久久久WWW成人免费精品| 伊人久久大香线蕉成人| 久久人与动人物a级毛片| 精品久久久久久亚洲精品| 久久精品?ⅴ无码中文字幕| 99久久免费国产精品特黄| 狠狠色噜噜狠狠狠狠狠色综合久久| 99久久精品国产毛片| 久久久久亚洲AV无码专区首JN| 大伊人青草狠狠久久| 亚洲精品NV久久久久久久久久 | 国产A三级久久精品| 九九精品99久久久香蕉| 久久影院午夜理论片无码| 奇米综合四色77777久久| 日韩亚洲国产综合久久久| 99久久久精品免费观看国产| 99精品国产免费久久久久久下载| 91久久精品国产成人久久| 久久99精品国产麻豆| 久久久久久精品免费看SSS| 九九热久久免费视频| 国产精品久久久久…| 色欲综合久久中文字幕网| 91麻精品国产91久久久久| 人妻无码精品久久亚瑟影视 | 亚洲综合伊人久久大杳蕉| 7777久久亚洲中文字幕| 亚洲国产成人精品久久久国产成人一区二区三区综 | 国产成人精品久久亚洲| 久久综合精品国产二区无码| 欧美一级久久久久久久大片| 欧美亚洲另类久久综合| 成人资源影音先锋久久资源网| 漂亮人妻被中出中文字幕久久| 久久www免费人成精品香蕉| 999久久久免费国产精品播放|