• <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>
            Impossible is nothing  
              愛(ài)過(guò)知情重醉過(guò)知酒濃   花開花謝終是空   緣份不停留像春風(fēng)來(lái)又走   女人如花花似夢(mèng)
            公告
            日歷
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345
            統(tǒng)計(jì)
            • 隨筆 - 8
            • 文章 - 91
            • 評(píng)論 - 16
            • 引用 - 0

            導(dǎo)航

            常用鏈接

            留言簿(4)

            隨筆分類(4)

            隨筆檔案(8)

            文章分類(77)

            文章檔案(91)

            相冊(cè)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

             

            看C++ Templates 16.1 Named Template Arguments

            書中的例子實(shí)現(xiàn)手法使用多重/虛擬繼承, 實(shí)現(xiàn)手法感覺(jué)比較詭秘. 但是至少告訴我是可以實(shí)現(xiàn)的.

            于是干脆自己也練了練手,  博君一笑. 只在VC7.1下測(cè)試過(guò), VC6也許可能可以迂回實(shí)現(xiàn), 但是估計(jì)工作量太大.

            1. 首先需要一個(gè)基本的 If 語(yǔ)句.

            template <bool, class T, class U>
            struct if_
            {
                typedef T type;
            };

            template<class T, class U>
            struct if_<false,  T,  U>
            {
                typedef U type;
            };

            2. 然后使用一個(gè) type_selector meta-function, N表示第幾個(gè)默認(rèn)參數(shù)(注意我的默認(rèn)Policy參數(shù)DefaultPolicyArgs里面有一個(gè)meta data, 為0. 如果是用戶定義的Policy, 那么形如Policy2_is的模板類里面有一個(gè)meta data為2. 這個(gè)數(shù)字主要是用于定位.

            最后的DefaultType是當(dāng)掃描一遍, 發(fā)現(xiàn)沒(méi)有任何對(duì)應(yīng)N位置的自定義Policy參數(shù), 那么就取這個(gè)為默認(rèn)值, 結(jié)束遞歸.(下面的4個(gè)void的特化版本就是干這個(gè)的)

            template<
                int N,
                class T1,
                class T2,
                class T3,
                class T4,
                class DefaultType>
            struct type_selector
            {
                typedef typename if_ <
                    (T1::value == N),
                    T1,
                    type_selector<N, T2, T3, T4, void, DefaultType> >::type eval_type;

                typedef typename eval_type::type type;
            };

            //shift以后, 如果都是默認(rèn)值, 遞歸會(huì)來(lái)到這里, 結(jié)束.
            template<
                int N,
                class DefaultType>
            struct type_selector<N, void, void, void, void, DefaultType>
            {
                typedef DefaultType type;
            };

            struct DefaultPolicy1 {};
            struct DefaultPolicy2 {};
            struct DefaultPolicy3 {
              public:
                static void doPrint() {
                    std::cout << "DefaultPolicy3::doPrint()\n";
                }
            };
            class DefaultPolicy4 {};

            struct  DefaultPolicyArgs {
                static const int value = 0;
            };


            template <typename Policy>
            struct Policy1_is
            {
                typedef Policy type;
                static const int value = 1;
            };

            template <typename Policy>
            struct Policy2_is
            {
                typedef Policy type;
                static const int value = 2;
            };


            template <typename Policy>
            struct Policy3_is
            {
                typedef Policy type;
                static const int value = 3;
            };


            template <typename Policy>
            struct Policy4_is
            {
                typedef Policy type;
                static const int value = 4;
            };


            template<class T1, class T2, class T3, class T4>
            struct PolicySelector
            {
                typedef typename type_selector<1, T1, T2, T3, T4, DefaultPolicy1>::type P1;
                typedef typename type_selector<2, T1, T2, T3, T4, DefaultPolicy2>::type P2;
                typedef typename type_selector<3, T1, T2, T3, T4, DefaultPolicy3>::type P3;
                typedef typename type_selector<4, T1, T2, T3, T4, DefaultPolicy4>::type P4;
            };
                

            template <typename T1 = DefaultPolicyArgs,
                      typename T2 = DefaultPolicyArgs,
                      typename T3 = DefaultPolicyArgs,
                      typename T4 = DefaultPolicyArgs>
            class BreadSlicer {
                typedef typename PolicySelector<T1, T2, T3, T4> Policies;
                
              public:
                void print () {
                    std::cout << typeid(Policies::P3).name() << std::endl;
                    Policies::P3::doPrint();
                }
                void  print_2()
                {
                    std::cout << typeid(Policies::P2).name() << std::endl;
                    Policies::P2::print_2();
                }
                //...
            };


            //下面的就是測(cè)試代碼了.

            class CustomPolicy {
              public:    
                static void doPrint() {
                    std::cout << "CustomPolicy::doPrint()\n";
                }
            };

            class CustomPolicy2 {
            public:
                static void print_2()
                {
                    std::cout << "Here is CustomPolicy2 instance" << std::endl;
                }
            };
                
            int main()
            {
                BreadSlicer<> bc1;
                bc1.print();

                BreadSlicer< Policy3_is<CustomPolicy>,
                    Policy2_is<CustomPolicy2> > bc2;
                
                bc2.print();
                return 0;
            }

            上面那個(gè)帖子的實(shí)現(xiàn)手法不太好, 當(dāng)client使用的時(shí)候, 還是需要

             BreadSlicer< Policy3_is<CustomPolicy>, Policy2_is<CustomPolicy2> > bc;

            復(fù)雜的嵌套模板語(yǔ)法, 如果能夠去掉PolicyN_is, 例如可以這樣

             BreadSlicer<> bc2;  //全部默認(rèn)policies

            如果定制其中的policy2, 這樣
             BreadSlicer< CustomPolicy2 > bc;

            如果需要定制2, 3, 這樣

             BreadSlicer< CustomPolicy2, CustomPolicy3 > bc;

            與順序無(wú)關(guān), 先寫3, 再寫2也可以
             BreadSlicer< CustomPolicy3, CustomPolicy2 > bc;

            那就更加簡(jiǎn)單了.

            幸運(yùn)的是, 這也是可以實(shí)現(xiàn)的, 而且與前面的帖子相比, 這個(gè)新的實(shí)現(xiàn)還直白簡(jiǎn)單, 使用起來(lái)由于直接使用Policy class作為參數(shù),
            而無(wú)需通過(guò)PolicyN_is這樣的包裹, 使用起來(lái)也更加優(yōu)雅.

            還是看看代碼:

            1. 同上, 定義一個(gè)if語(yǔ)句.

            template <bool, class T, class U>
            struct if_
            {
                typedef typename T type;
            };

            template<class T, class U>
            struct if_<false,  T,  U>
            {
                typedef typename U type;
            };

            2. 定義一個(gè)wrapper, 使得 type_wrapper<T>::type 有效(為T). 因?yàn)橹苯邮褂?T::type 可能遇到T根本沒(méi)有type這個(gè)typedef內(nèi)嵌類型.

            template<class T>
            struct type_wrapper
            {
                typedef T type;
            };

            struct  DefaultPolicyArgs {
                static const int value = 0;            //特殊meta-data, 為0表示默認(rèn)參數(shù)
            };


            3. 然后使用一個(gè) type_selector meta-function, N表示第幾個(gè)默認(rèn)參數(shù)(注意我的默認(rèn)Policy參數(shù)DefaultPolicyArgs里面有一個(gè)meta data, 為0. 如果是用戶定義的Policy, 那么它也應(yīng)該定義一個(gè)meta data,. 這個(gè)數(shù)字主要是用于告訴selector它是想覆蓋第幾個(gè)默認(rèn)的policy參數(shù).

            最后的DefaultType是當(dāng)掃描一遍, 發(fā)現(xiàn)沒(méi)有任何對(duì)應(yīng)N位置的自定義Policy參數(shù), 那么就取這個(gè)為默認(rèn)值, 結(jié)束遞歸.(下面的4個(gè)DefaultPolicyArgs的特化版本就是干這個(gè)的)

            與前面的一個(gè)版本相比, 我不再使用void, 而是使用DefaultPolicyArgs來(lái)填充, 這樣在大部分情況下匹配速度要快. (指編譯速度)

            template<
                int N,
                class T1,
                class T2,
                class T3,
                class T4,
                class DefaultType>
            struct type_selector
            {
                typedef typename if_ <
                    (T1::value == N),
                    type_wrapper<T1>,
                    type_selector<N, T2, T3, T4, DefaultPolicyArgs, DefaultType> >::type eval_type;

                typedef typename eval_type::type type;
            };

            //shift以后最終來(lái)到這里, 結(jié)束遞歸
            template<
                int N,
                class DefaultType>
            struct type_selector<N, DefaultPolicyArgs, DefaultPolicyArgs, DefaultPolicyArgs, DefaultPolicyArgs, DefaultType>
            {
                typedef DefaultType type;
            };


            struct DefaultPolicy1 {};
            struct DefaultPolicy2 {};
            struct DefaultPolicy3 {
              public:
                static void doPrint() {
                    std::cout << "DefaultPolicy3::doPrint()\n";
                }
            };
            class DefaultPolicy4 {};


            template<class T1, class T2, class T3, class T4>
            struct PolicySelector
            {
                typedef typename type_selector<1, T1, T2, T3, T4, DefaultPolicy1>::type P1;
                typedef typename type_selector<2, T1, T2, T3, T4, DefaultPolicy2>::type P2;
                typedef typename type_selector<3, T1, T2, T3, T4, DefaultPolicy3>::type P3;
                typedef typename type_selector<4, T1, T2, T3, T4, DefaultPolicy4>::type P4;
            };
               


            template <typename T1 = DefaultPolicyArgs,
                      typename T2 = DefaultPolicyArgs,
                      typename T3 = DefaultPolicyArgs,
                      typename T4 = DefaultPolicyArgs>
            class BreadSlicer {
                typedef typename PolicySelector<T1, T2, T3, T4> Policies;
               
              public:
                void print () {
                    std::cout << typeid(Policies::P3).name() << std::endl;
                    Policies::P3::doPrint();
                }
                void  print_2()
                {
                    std::cout << typeid(Policies::P2).name() << std::endl;
                    Policies::P2::print_2();
                }
                //...
            };


            class CustomPolicy2 {
            public:
                static const int value = 2;     //關(guān)鍵的修改在此, 這個(gè)是實(shí)現(xiàn)定制Policy時(shí)需要提供的meta data
                static void print_2()
                {
                    std::cout << "Here is CustomPolicy2 instance" << std::endl;
                }
            };

            class CustomPolicy3 {
              public:   
                static const int value = 3;     //meta data, 同上, 3表示這個(gè)代表的是用了替換Policy3
                static void doPrint() {
                    std::cout << "CustomPolicy3::doPrint()\n";
                }
            };


            //這樣, PolicyN_is 就沒(méi)有了, 唯一的要求就是, 當(dāng)實(shí)現(xiàn)custom policy的時(shí)候, 別忘了在其中定義一個(gè)叫做
            value的整形常量, N代表替換哪個(gè)默認(rèn)的policy參數(shù).

            int main()
            {
                BreadSlicer<> bc1;                                 //全部默認(rèn)
                bc1.print();

                BreadSlicer< CustomPolicy2, CustomPolicy3 > bc2;   //2,3定制, 是不是干凈一些?
               
                bc2.print();
                bc2.print_2();
                std::cout << std::flush;
                return 0;
            }
            posted on 2006-02-27 23:14 笑笑生 閱讀(349) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++語(yǔ)言
             
            Copyright © 笑笑生 Powered by: 博客園 模板提供:滬江博客
            久久久久99精品成人片三人毛片| 国产精品无码久久综合网| 久久99精品久久久大学生| 午夜精品久久久久久99热| 国产亚洲美女精品久久久久狼| 好属妞这里只有精品久久| 久久久黄片| 国产成人精品白浆久久69 | 久久精品亚洲欧美日韩久久| 无码精品久久一区二区三区 | 久久久久免费视频| 亚洲精品美女久久久久99| 国内精品久久久久久久影视麻豆| 久久国产精品免费| 久久久久亚洲av无码专区导航| 国产精品亚洲综合专区片高清久久久| 国产精品乱码久久久久久软件| 国内精品久久久久久99蜜桃| 亚洲精品国精品久久99热| 精品综合久久久久久888蜜芽| 亚洲精品久久久www| 国产成人精品久久亚洲高清不卡 | 久久99热国产这有精品| 日本五月天婷久久网站| 久久国产精品波多野结衣AV| 狠狠色婷婷综合天天久久丁香| 久久久噜噜噜久久中文字幕色伊伊 | 国内精品久久久久久99蜜桃 | 久久久久九国产精品| 久久国产精品99精品国产987| 亚洲日本va中文字幕久久| 亚洲国产精品成人久久蜜臀| 国产成人精品久久| 国产激情久久久久影院小草| 久久精品国内一区二区三区| 久久国产精品77777| 久久久久人妻一区精品性色av| 一本一本久久a久久综合精品蜜桃| 污污内射久久一区二区欧美日韩| 亚洲Av无码国产情品久久| 伊人 久久 精品|