Team LiB
Previous Section Next Section

Chapter 16. Templates and Generic Programming

第十六章 模板和泛型编程


Section 16.1 Template Definitions


Section 16.2 Instantiation


Section 16.3 Template Compilation Models


Section 16.4 Class Template Members


Section 16.5 A Generic Handle Class


Section 16.6 Template Specializations


Section 16.7 Overloading and Function Templates


Chapter Summary


Defined Terms


Generic programming involves writing code in a way that is independent of any particular type. When we use a generic program we supply the type(s) or value(s) on which that instance of the program will operate. The library containers, iterators, and algorithms described in Part II are examples of generic programming. There is a single definition of each container, such as vector, but we can define many different kinds of vectors that differ by the element type that the vector contains.

所谓泛型编程就是以独立于任何特定类型的方式编写代码。使用泛型程序时,我们需要提供具体程序实例所操作的类型或值。第二部分中描述的标准库的容器、迭代器和算法都是泛型编程的例子。每种容器(如 vector)都有单一的定义,但可以定义许多不同种类的 vector,它们的区别在于所包含的元素类型。

Templates are the foundation of generic programming. We can, and have, used templates without understanding how they are defined. In this chapter we'll see how we can define our own template classes and functions.


Generic programming, like object-oriented programming, relies on a form of polymorphism. The polymorphism in OOP applies at run time to classes related by inheritance. We can write code that uses such classes in ways that ignore the type differences among the base and derived classes. As long as we use references or pointers to the base type, we can use the same code on objects of the base type or a type derived from that type.


Generic programming lets us write classes and functions that are polymorphic across unrelated types at compile time. A single class or function can be used to manipulate objects of a variety of types. The standard library containers, iterators, and algorithms are good examples of generic programming. The library defines each of the containers, iterators, and algorithms in a type-independent manner. We can use library classes and functions on most any kind of type. For example, we can define a vector of Sales_item objects even though the designers of vector could have had no knowledge of our application-specific class.

在泛型编程中,我们所编写的类和函数能够多态地用于跨越编译时不相关的类型。一个类或一个函数可以用来操纵多种类型的对象。标准库中的容器、迭代器和算法是很好的泛型编程的例子。标准库用独立于类型的方式定义每个容器、迭代器和算法,因此几乎可以在任意类型上使用标准库的类和函数。例如,虽然 vector 的设计者不可能了解应用程序特定的类,但我们能够定义 Sales_item 对象组成的 vector

In C++, templates are the foundation for generic programming. A template is a blueprint or formula for creating a class or a function. For example, the standard library defines a single class template that defines what it means to be a vector. That template is used to generate any number of type-specific vector classesfor example, vector<int> or vector<string>. Part II showed how to use generic types and functions; this chapter shows how we can define our own templates.

在 C++ 中,模板是泛型编程的基础。模板是创建类或函数的蓝图或公式。例如,标准库定义了一个类模板,该模板定义了 vector 的含义,它可以用于产生任意数量的特定类型的 vector 类,例如,vector<int>vector<string>。本书第二部分介绍了怎样使用泛型类型和泛型函数,本章将介绍怎样自定义模板。

Team LiB
Previous Section Next Section