模板函数与函数模板

1.函数指针——指针函数

函数指针的重点是指针。表示的是一个指针,它指向的是一个函数,例子:

int   (*pf)();

指针函数的重点是函数。表示的是一个函数,它的返回值是指针。例子:

int*   fun();

 

2.数组指针——指针数组

数组指针的重点是指针。表示的是一个指针,它指向的是一个数组,例子:

int   (*pa)[8];

指针数组的重点是数组。表示的是一个数组,它包含的元素是指针。例子;

int*   ap[8];

 

3.类模板——模板类(class   template——template   class)

类模板的重点是模板。表示的是一个模板,专门用于产生类的模子。例子:

template   <typename   T>

class   Vector

{

            …

};

使用这个Vector模板就可以产生很多的class(类),Vector <int> 、Vector <char> 、Vector <   Vector <int>   > 、Vector <Shape*> ……。

模板类的重点是类。表示的是由一个模板生成而来的类。例子:

上面的Vector <int> 、Vector <char> 、……全是模板类。

这两个词很容易混淆,我看到很多文章都将其用错,甚至一些英文文章也是这样。将他们区分开是很重要的,你也就可以理解为什么在定义模板的头文件.h时,模板的成员函数实现也必须写在头文件.h中,而不能像普通的类(class)那样,class的声明(declaration)写在.h文件中,class的定义(definition)写在.cpp文件中。请参照Marshall   Cline的《C++   FAQ   Lite》中的[34]   Container   classes   and   templates中的[34.12]   Why   can 't   I   separate   the   definition   of   my   templates   class   from   it 's   declaration   and   put   it   inside   a   .cpp   file?   URL地址是http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12

我将几句关键的段落摘录如下,英文很好理解:

In   order   for   the   compiler   to   generate   the   code,   it   must   see   both   the   template   definition   (not   just   declaration)   and   the   specific   types/whatever   used   to   "fill   in "   the   template.   For   example,   if   you 're   trying   to   use   a   Foo <int> ,   the   compiler   must   see   both   the   Foo   template   and   the   fact   that   you 're   trying   to   make   a   specific   Foo <int> .  

Suppose   you   have   a   template   Foo   defined   like   this:  

  template <class   T>
  class   Foo   {
  public:
      Foo();
      void   someMethod(T   x);
  private:
      T   x;
  };  

Along   with   similar   definitions   for   the   member   functions:  

  template <class   T>
  Foo <T> ::Foo()
  {
      ...
  }
 
  template <class   T>
  void   Foo <T> ::someMethod(T   x)
  {
      ...
  }  

Now   suppose   you   have   some   code   in   file   Bar.cpp   that   uses   Foo <int> :  

  //   Bar.cpp
 
  void   blah_blah_blah()
  {
      ...
      Foo <int>   f;
      f.someMethod(5);
      ...
  }  

Clearly   somebody   somewhere   is   going   to   have   to   use   the   "pattern "   for   the   constructor   definition   and   for   the   someMethod()   definition   and   instantiate   those   when   T   is   actually   int.   But   if   you   had   put   the   definition   of   the   constructor   and   someMethod()   into   file   Foo.cpp,   the   compiler   would   see   the   template   code   when   it   compiled   Foo.cpp   and   it   would   see   Foo <int>   when   it   compiled   Bar.cpp,   but   there   would   never   be   a   time   when   it   saw   both   the   template   code   and   Foo <int> .   So   by   rule   above,   it   could   never   generate   the   code   for   Foo <int> ::someMethod().  

关于一个缺省模板参数的例子:

template   <typename   T   =   int>

class   Array

{

            …

};

第一次我定义这个模板并使用它的时候,是这样用的:

Array   books;//我认为有缺省模板参数,这就相当于Array <int>   books

上面的用法是错误的,编译不会通过,原因是Array不是一个类。正确的用法是Array <>   books;

这里Array <> 就是一个用于缺省模板参数的类模板所生成的一个具体类。

 

4.函数模板——模板函数(function   template——template   function)

函数模板的重点是模板。表示的是一个模板,专门用来生产函数。例子:

template   <typename   T>

void   fun(T   a)

{

            …

}

在运用的时候,可以显式(explicitly)生产模板函数,fun <int> 、fun <double> 、fun <Shape*> ……。

也可以在使用的过程中由编译器进行模板参数推导,帮你隐式(implicitly)生成。

fun(6);//隐式生成fun <int>

fun(8.9);//隐式生成fun <double>

fun(‘a’);//   隐式生成fun <char>

Shape*   ps   =   new   Cirlcle;

fun(ps);//隐式生成fun <Shape*>

 

模板函数的重点是函数。表示的是由一个模板生成而来的函数。例子:

上面显式(explicitly)或者隐式(implicitly)生成的fun <int> 、fun <Shape*> ……都是模板函数。

关于模板本身,是一个非常庞大的主题,要把它讲清楚,需要的不是一篇文章,而是一本书,幸运的是,这本书已经有了:David   Vandevoorde,   Nicolai   M.   Josuttis写的《C++   Templates:   The   Complete   Guide》。可惜在大陆买不到纸版,不过有一个电子版在网上流传。

 

模板本身的使用是很受限制的,一般来说,它们就只是一个产生类和函数的模子。除此之外,运用的领域非常少了,所以不可能有什么模板指针存在的,即指向模板的指针,这是因为在C++中,模板就是一个代码的代码生产工具,在最终的代码中,根本就没有模板本身存在,只有模板具现出来的具体类和具体函数的代码存在。

但是类模板(class   template)还可以作为模板的模板参数(template   template   parameter)使用,在Andrei   Alexandrescu的《Modern   C++   Design》中的基于策略的设计(Policy   based   Design)中大量的用到。

template <   typename   T,   template <typename   U>   class   Y>

class   Foo

{

            …

};

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值