Fork me on GitHub

模板类与类模板、函数模板与模板函数等的区别

  在C++中有好几个这样的术语,但是我们很多时候用的并不正确,几乎是互相替换混淆使用。下面我想彻底辨清几个术语,这样就可以避免很多概念上的混淆和使用上的错误。这几个词是:  

  • 函数指针——指针函数
  • 数组指针——指针数组
  • 类模板——模板类
  • 函数模板——模板函数

  最终在使用中,我们就可以让它们实至名归,名正言顺。  

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

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

  int   (*pf)();  

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

  int*   fun();  

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

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

  int   (*pa)[8];  

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

  int*   ap[8];  

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

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

1 template <typename T>
2 class Vector
3 {
4     ...
5 };

  使用这个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: 

1 template<class   T>
2 class   Foo {
3 public:
4     Foo();
5     void   someMethod(T   x);
6 private:
7     T   x;
8 };

  Along with similar definitions for the member functions:   

复制代码
 1 template<class   T>
 2 Foo<T>::Foo()
 3 {
 4     ...
 5 }
 6 
 7 template<class   T>
 8 void   Foo<T>::someMethod(T   x)
 9 {
10     ...
11 }
复制代码

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

  //   Bar.cpp  

1 void   blah_blah_blah()  
2 {  
3   ...  
4   Foo<int>   f;  
5   f.someMethod(5);  
6   ...  
7 }    

  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().  

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

1 template   <typename   T = int>
2 class   Array
3 {
4   ...
5 };

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

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

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

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

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

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

1 template   <typename   T>  
2 void   fun(T   a)  
3 {  
4   ...
5 }  

  在运用的时候,可以显式(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)中大量的用到。  

1 template<   typename   T,   template<typename   U>   class   Y>  
2 class   Foo  
3 {  
4   ...
5 };   

  从文章的讨论中,可以看到,名字是非常重要的,如果对名字的使用不恰当的话,会引起很多的麻烦和误解。我们在实际的程序中各种标识符的命名也是一门学问,为了清晰易懂,有时候还是需要付出一定的代价。  

  最后提醒:在本文的几个术语中,语言的重心在后面,前面的词是作为形容词使用的。

 

 

  转自:http://blog.sina.com.cn/s/blog_8b6cb0930100xair.html


作者:wangduo

出处:http://www.cnblogs.com/wangduo/

本博客中未标明转载的文章归作者wangduo和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  手机扫码访问:

posted @   wangduo  阅读(17188)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示