Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2568215
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-05-19 23:16:06

    代码先:后面想个实用的例子【当然视频里的也实用,不过想自己想下先】:

点击(此处)折叠或打开

  1. #ifndef _TRAIT_
  2. #define _TRAIT_

  3. /**
  4.  * @brief 萃取 - 根据模板特化的参数匹配相应的模版类,内部萃取到本质的数据类型.
  5.  */
  6. namespace trait
  7. {
  8.     template <typename T>
  9.     class type_addtion
  10.     {
  11.     public:
  12.         typedef T valueType;
  13.         typedef T * pointer;
  14.         typedef T & reference;
  15.         typedef const T * const_pointer;
  16.         typedef const T & const_reference;
  17.     };

  18.     template <typename T>
  19.     class type_addtion <T *>
  20.     {
  21.     public:
  22.         typedef T valueType;
  23.         typedef T * pointer;
  24.         typedef T & reference;
  25.         typedef const T * const_pointer;
  26.         typedef const T & const_reference;
  27.     };

  28.     template <typename T>
  29.     class type_addtion <const T *>
  30.     {
  31.     public:
  32.         typedef T valueType;
  33.         typedef T * pointer;
  34.         typedef T & reference;
  35.         typedef const T * const_pointer;
  36.         typedef const T & const_reference;
  37.     };

  38.     template <typename T>
  39.     class type_addtion <T **>
  40.     {
  41.     public:
  42.         typedef T valueType;
  43.         typedef T * pointer;
  44.         typedef T & reference;
  45.         typedef const T * const_pointer;
  46.         typedef const T & const_reference;
  47.     };

  48.     template <typename T>
  49.     class type_addtion <T &>
  50.     {
  51.     public:
  52.         typedef T valueType;
  53.         typedef T * pointer;
  54.         typedef T & reference;
  55.         typedef const T * const_pointer;
  56.         typedef const T & const_reference;
  57.     };

  58.     template <typename T>
  59.     class type_addtion <const T &>
  60.     {
  61.     public:
  62.         typedef T valueType;
  63.         typedef T * pointer;
  64.         typedef T & reference;
  65.         typedef const T * const_pointer;
  66.         typedef const T & const_reference;
  67.     };

  68.     ///< 完全特化
  69.     template <>
  70.     class type_addtion <void>
  71.     {
  72.     public:
  73.         typedef void valueType;
  74.         typedef void * pointer;
  75.         typedef const void * const_pointer;
  76.     };
  77. }

  78. #endif    //_TRAIT_
trait.c

点击(此处)折叠或打开

  1. #include "trait.h"
  2. #include <iostream>

  3. using namespace std;

  4. int main(int argc, char ** argv)
  5. {
  6.     ///< 不管是int,还是int **作为模版参数,我们都可以萃取到变量、指针、引用类型.
  7.     trait::type_addtion<int> iData;
  8.     trait::type_addtion<int>::valueType i;
  9.     trait::type_addtion<int *>::valueType iso;
  10.     trait::type_addtion<int>::pointer ip;
  11.     trait::type_addtion<int **>::pointer ipso;

  12.     return 0;
  13. }
阅读(2929) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

Seed-L2013-05-21 09:31:13

有点类似STL的做法~~