您的位置:首页 > 其它

模板函数、函数模板,模板类、类模板

2009-11-10 17:56 155 查看
名正言顺
中国有句古话:名不正则言不顺。一样东西,名字如果用的不好,不但听起来不舒服,而且真实的本质也容易被掩盖。如果一样东西有一个好名字,我们就能更容易的记住它和理解它。

在现代汉语中,许多词的重点都在后面,比如下面我们经常看到的两个词语。

冰雪聪明。

聪明冰雪。

冰雪聪明强调的是聪明,她像冰雪一样的聪明。

聪明冰雪强调的是冰雪,她很聪明,看上去更是冰雪般的玲珑剔透纯洁。

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

这几个词是:

函数指针——指针函数

数组指针——指针数组

类模板——模板类

函数模板——模板函数

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

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

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

int (*pf)();

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

int* fun();

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

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

int (*pa)[8];

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

int* ap[8];

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

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

template <class 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; //实例化类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

{



};

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

最后提醒:在本文的几个术语中,语言的重心在后面,前面的词是作为形容词使用的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: