标准模板库简介(二)

Cppasm 2004-01-18 10:38:11
Concepts and Modeling
One very important question to ask about any template function, not just about STL algorithms, is what the set of types is that may correctly be substituted for the formal template parameters. Clearly, for example, int* or double* may be substituted for find's formal template parameter InputIterator. Equally clearly, int or double may not: find uses the expression *first, and the dereference operator makes no sense for an object of type int or of type double. The basic answer, then, is that find implicitly defines a set of requirements on types, and that it may be instantiated with any type that satisfies those requirements. Whatever type is substituted for InputIterator must provide certain operations: it must be possible to compare two objects of that type for equality, it must be possible to increment an object of that type, it must be possible to dereference an object of that type to obtain the object that it points to, and so on.
概念和模型
关于模板函数的一个非常重要的问题,不仅是关于STL算法,就是可以正确替换形式模板参数的类型的集合.很明显,例如,int*或者double*可以用来替换find的形式模板参数InputIterator.同样明显的是,int或者double就不可以:find使用表达式*first,解析引用(dereference)操作符对于类型为int或者double的对象并没有任何意义.因此,基本的答案就是find隐含的定义了对于类型的要求,如果这些要求得到满足那么就可以实例化.不管什么类型替换了InputIterator都必须提供一定的操作:必须能够可以比较该类型的两个对象是否相等,必须能够对该类型的一个对象进行增加操作,必须能够对该类型的一个对象进行解析引用操作以获得它所指向的对象等等.

Find isn't the only STL algorithm that has such a set of requirements; the arguments to for_each and count, and other algorithms, must satisfy the same requirements. These requirements are sufficiently important that we give them a name: we call such a set of type requirements a concept, and we call this particular concept Input Iterator. We say that a type conforms to a concept, or that it is a model of a concept, if it satisfies all of those requirements. We say that int* is a model of Input Iterator because int* provides all of the operations that are specified by the Input Iterator requirements.
find并不是有这种要求的唯一一个STL算法;for_each和count以及其它算法的参数都必须满足相同的要求.这些要求都是相当的重要,所以我们给它们一个名字:我们将这种类型要求称为一个概念(concept),并将这种特殊的概念(concept)称为Input_Iterator.我们说一个类型遵循一个概念,或者它是一个概念的一个模型,如果它满足所有那些要求的话.我们说int*是Input_iterator的一个模型,因为int*提供了Input_Iterator要求所指定的所有操作.

Concepts are not a part of the C++ language; there is no way to declare a concept in a program, or to declare that a particular type is a model of a concept. Nevertheless, concepts are an extremely important part of the STL. Using concepts makes it possible to write programs that cleanly separate interface from implementation: the author of find only has to consider the interface specified by the concept Input Iterator, rather than the implementation of every possible type that conforms to that concept. Similarly, if you want to use find, you need only to ensure that the arguments you pass to it are models of Input Iterator. This is the reason why find and reverse can be used with lists, vectors, C arrays, and many other types: programming in terms of concepts, rather than in terms of specific types, makes it possible to reuse software components and to combine components together.
概念并不是C++语言的一部分;没有办法在一个程序中声明一个概念,或者声明一个特定类型为一个概念的一个模型.然而,概念是STL的一个极度重要的部分.使用概念使得编写将接口与实现明确分离开的程序成为可能:find的作者只需考虑由概念Input_Iterator所指定的接口,而不用考虑遵循该概念的每个可能的类型.相似的,如果你想使用find,你只需确保你所传递的参数是Input_Iterator的模型.这就是为什么find和reverse可以用于list,vector,C数组和许多其它类型的原因:根据概念(concept)而不是特定类型来编程使得我们可以重用软件组件和将组件联合在一起.
Refinement
Input Iterator is, in fact, a rather weak concept: that is, it imposes very few requirements. An Input Iterator must support a subset of pointer arithmetic (it must be possible to increment an Input Iterator using prefix and postfix operator++), but need not support all operations of pointer arithmetic. This is sufficient for find, but some other algorithms require that their arguments satisfy additional requirements. Reverse, for example, must be able to decrement its arguments as well as increment them; it uses the expression --last. In terms of concepts, we say that reverse's arguments must be models of Bidirectional Iterator rather than Input Iterator.
...全文
45 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧