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 肥仔 阅读(694) 评论(0)  编辑 收藏 引用 所属分类: C++ 模板


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理