C++ template 的 type traits 代码

来源:互联网 发布:脚本录制软件 编辑:程序博客网 时间:2024/05/17 23:46

//Author : cppgp
//Email  : cppgp@163.com
//Time   : 2007 03 08

//功能 : 测试 C++ template 的 traits 技巧
//版权 : 可任意转载、修改、使用,转载注明原作者姓名

//vc 6.0 下必须去掉 label_traits 的特化版本才能通过编译链接
//gcc  下面 label_traits 特化版本测试通过

#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;
}

原创粉丝点击