• <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>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            STL中迭代器traits技巧學習 : 一個簡單的測試程序

            // testTraits_02.cpp : Defines the entry point for the console application.

            //

             

            //Author : cppgp

            //Email  : cppgp@163.com

            //Time   : 2007 03 08

             

            //功能 : 測試 C++ template traits 技巧

            //版權 : 可任意轉載、拷貝、修改、使用,但請注明原作者姓名

             

            //vc 6.0 下必須去掉 label_traits 的特化版本才能通過編譯鏈接

            //gcc  下面 label_traits 特化版本測試通過

            #include "StdAfx.h"

            #include <iostream>

             

            using namespace std;

             

            //下面定義五種測試標簽

             

            struct label_1{};

            struct label_2{};

            struct label_3 : public label_2{};

            struct label_4 : public label_3{};

            struct label_5 : public label_4{};

             

            //下面定義五種標簽對應的模板類型

             

            //另注 : _Tp 對應的 value_type 沒有用到

            //只是做為一種型別存在而已

            //當然你可以不要它!

            template<class _Tp>

            struct lable_1_type

            {

                typedef label_1 label_type;

                typedef _Tp value_type;

            };

             

            template<class _Tp>

            struct lable_2_type

            {

                typedef label_2 label_type;

                typedef _Tp value_type;

            };

             

            template<class _Tp>

            struct lable_3_type

            {

                typedef label_3 label_type;

                typedef _Tp value_type;

            };

             

            template<class _Tp>

            struct lable_4_type

            {

                typedef label_4 label_type;

                typedef _Tp value_type;

            };

             

            template<class _Tp>

            struct lable_5_type

            {

                typedef label_5 label_type;

                typedef _Tp value_type;

            };

             

            //下面是特性萃取 : 分別是泛化和特化版本

             

            template <class label>

            struct label_traits

            {

                typedef typename label::label_type label_type;

                typedef typename label::value_type value_type;

            };

             

            #if 0 //如果是 gcc 0 修改為 1 即可

             

            template <class label>

            struct label_traits<label*>

            {

                typedef label_5 label_type;

                typedef label value_type;

            };

             

            template <class label>

            struct label_traits<const label*>

            {

                typedef label_5 label_type;

                typedef label value_type;

            };

             

            #endif

             

            //下面是生成標簽類型的臨時變量,其本質如同 int() 生成 int 臨時變量一樣

             

            template <class label>

            inline typename label_traits<label>::label_type

            label_type(const label&)

            {

                typedef typename label_traits<label>::label_type Label_Type;

                return Label_Type();

            }

             

            //下面這個是針對不同標簽寫的對應重載函數

             

            template<class label>

            inline void _TestFunc(label,label_1)

            {

                cout<<"here label_1"<<endl;

            }

             

            template<class label>

            inline void _TestFunc(label,label_2)

            {

                cout<<"here label_2"<<endl;

            }

             

            template<class label>

            inline void _TestFunc(label,label_3)

            {

                cout<<"here label_3"<<endl;

            }

             

            template<class label>

            inline void _TestFunc(label,label_4)

            {

                cout<<"here label_4"<<endl;

            }

             

            template<class label>

            inline void _TestFunc(label,label_5)

            {

                cout<<"here label_5"<<endl;

            }

             

            //下面這個是上面函數的上層封裝調用

             

            template<class label>

            inline void TestFunc(label& l)

            {

                _TestFunc(l,label_type(l));

            }

             

            //下面是測試主程序

             

            class TestClass

            {

            };

             

            int main()

            {

                //定義標簽對象

               

                cout<<"\r\n\r\nbegin test ...\r\n\r\n";

               

                //原生

                {

                   cout<<"int :\n";

                   lable_1_type<int> l1;

                   lable_2_type<int> l2;

                   lable_3_type<int> l3;

                   lable_4_type<int> l4;

                   lable_5_type<int> l5;

                   TestFunc(l1);

                   TestFunc(l2);

                   TestFunc(l3);

                   TestFunc(l4);

                   TestFunc(l5);

                   cout<<"\r\n\r\n";

                }

               

                //自定義類型

                {

                   cout<<"test class:\n";

                   lable_1_type<TestClass> l1;

                   lable_2_type<TestClass> l2;

                   lable_3_type<TestClass> l3;

                   lable_4_type<TestClass> l4;

                   lable_5_type<TestClass> l5;

                   TestFunc(l1);

                   TestFunc(l2);

                   TestFunc(l3);

                   TestFunc(l4);

                   TestFunc(l5);

                }

               

                cout<<"\r\ntest end...\r\n\r\n"<<endl;

               

                return 0;

            }

            //結束

            posted on 2008-11-09 01:35 肥仔 閱讀(721) 評論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            久久香蕉一级毛片| 久久久久人妻一区精品色| 久久久久人妻一区二区三区vr| 久久亚洲精品无码VA大香大香| 7777精品久久久大香线蕉 | 97热久久免费频精品99| 97久久精品午夜一区二区| 91精品日韩人妻无码久久不卡| 亚洲人成网站999久久久综合| 亚洲天堂久久久| 国产亚洲美女精品久久久久狼| 一本久道久久综合狠狠躁AV | 久久午夜无码鲁丝片| 日本欧美国产精品第一页久久| 久久免费的精品国产V∧| 久久精品成人免费网站| 无码任你躁久久久久久老妇App| 波多野结衣中文字幕久久| 久久精品人人做人人爽电影| 久久国产成人午夜AV影院| 97精品依人久久久大香线蕉97| 日韩精品久久久久久| 久久人爽人人爽人人片AV | 久久精品综合网| 亚洲成人精品久久| 激情伊人五月天久久综合| 久久精品国产亚洲av麻豆蜜芽| 无码乱码观看精品久久| 国产999精品久久久久久| 久久这里只有精品久久| 老色鬼久久亚洲AV综合| 久久香综合精品久久伊人| 色婷婷噜噜久久国产精品12p| 久久99精品九九九久久婷婷| 亚洲综合久久综合激情久久| 久久久久久亚洲Av无码精品专口| 久久久久久久女国产乱让韩| 麻豆av久久av盛宴av| 综合人妻久久一区二区精品| 99久久国产精品免费一区二区| 伊人久久大香线蕉av不变影院|