Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1676390
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类: C/C++

2012-06-30 17:03:51

原文:http://blog.csdn.net/bluedog/article/details/4715542

现如今才正真发现c++模板的强大,模板的强大在于不需要使用RTTI机制可以传递参数的类型信息,不过也有其遗憾的地方,就是对于引用和复制,模板并无法区分。例如

template

struct Test

{

typedef T Type;

static void PrintTemplateTypeName()

{

     cout << typeid(Type).name() <

}

};

 

如果我们分别用

第一组 Test,Test,Test其结果是相同的,总是输出 int

第二组 Test,Test 总是输出 int *

第三组 Test,Test 总是输出 int const *

此处如果对于 const int *  int * const还不了解的朋友可以这样来看

(a)const int *   <==> (const int)*  即指向常量int值的指针,它也可以表述为 int const *;

(b)int * const   <==> (int *) const 即是一个int型指针,不过这个指针不能改变,即不可以做 ++ -- 等操作。

所以很多在写常量字符串指针时写成

const char * str = "Hellow,C++";

这种写法应该说是不完善的,正确的写法应该如下

const char * const str = "Hellow ,C++!";

 

讲了这么多,其实跟我们今天所要讲的东东关系不是很大,我们只要了解到模板不能传递模板参数类型的引用和const修鉓定义。下面走入正题

为什么我们需要模板类支持可变模板参数?

我在写c++事件支持时,需要使用下面一种语法,事件需要一个委托定义。但是由于用户的委托类型是可变的,而且参数也是不一定的,假如这个委托模板类的名字叫delegate,我们好象不能同时写出下面这几种东东出来

delegate  ClickHandler;

delegate DrawHandler;

delegate DrawTextHandler;

因为类并不支持重载。事实上网上流传的FastDelegate也是使用了FastDelegate0,FastDelegate1这样的东东来区分不同数量的模板参数。

 

模板总是让我们不断发现它的强大。忽然我开悟了,完全可以让模板类支持不同数量的参数。下面给出我的代码,有代码有真相!

 

template<typename T>

struct is_non_template_param

{

    enum { have_template_param =1 };

};

 

template<>

struct is_non_template_param<void>

{

    enum { have_template_param =0 };

};

 

template<typename T1,typename T2,typename T3,typename T4,typename T5>

struct template_params

{

    enum { count = is_non_template_param::have_template_param +

                  is_non_template_param::have_template_param +

                  is_non_template_param::have_template_param +

                  is_non_template_param::have_template_param +

                  is_non_template_param::have_template_param };

};

 

template<typename T1 = void,

         typename T2 = void,

        typename T3 = void,

        typename T4 = void,

        typename T5 = void>

struct Delegate

{

    enum{ ParamsCount = template_params::count };

    int GetParamsCount()

    {

        return ParamsCount;

    }

 

};

 

int _tmain(int argc, _TCHAR* argv[])

{

    Delegate<int,double,char> d1;

    Delegate<int,int*,char*,int> d2;

    Delegate<void> d3;

   

    cout <<"d1 params count =" << d1.GetParamsCount()<

    cout <<"d2 params count =" << d2.GetParamsCount()<

    cout <<"d3 params count =" << d3.GetParamsCount()<

    return 0;

}

我们很容易地就实现了不定参数的模板类。呵呵

(function(w, d, g, J) { var e = J.stringify || J.encode; d[g] = d[g] || {}; d[g]['showValidImages'] = d[g]['showValidImages'] || function() { w.postMessage(e({'msg': {'g': g, 'm':'s'}}), location.href); } })(window, document, '__huaban', JSON);
阅读(2750) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~