在通用编程/TMP 世界中,模型/策略和“概念”到底是什么?
我想在一处了解这三个概念的精确而简洁的定义。答案的质量应该取决于以下两点。
- 显示一个简单的代码片段来展示该概念/技术的用途和用途。
- 足够简单易懂,以便没有接触过该领域的程序员也能掌握。
注意:
可能有很多正确答案,因为每个概念都有许多不同的方面。 如果有很多好的答案,我最终会将问题转为 CW 并汇总答案。
-- 接受后编辑 --
Boost 有一篇关于通用编程概念的很好的文章
I'd like to know the precise yet succinct definitions of these three concepts in one place. The quality of the answer should depend on the following two points.
- Show a simple code snippet to show how and what the concept/technique is used for.
- Be simple enough to understand so that a programmer without any exposure to this area can grasp it.
Note:
There are probably many correct answers since each concept has many different facets. If there are a lot of good answers I will eventually turn the question into CW and aggregate the answers.
-- Post Accept Edit --
Boost has a nice article on generic programming concepts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
概念是对类型的一组要求。例如,您可以有一个名为“RandomAccessible”的概念,它要求在 O(1) 时间内实现
operator[](int)
的类型。由于概念从即将推出的 C++ 标准中被删除,它们仅作为文档无形地存在于 C++ 中。例如,您可以阅读SGI 对容器概念的描述。
当一种类型满足某个概念的所有要求时,您可以将其称为该概念的模型。例如,
std::vector
是 Container 概念的模型(或者等效地,std::vector
“models”Container)。最后,策略是一个行为单元,它可以与其他行为单元结合起来构建复杂的类。例如,假设您想构建两个类:一个固定大小的数组和一个动态调整大小的数组。这两个类都有很多共享功能,但只是存储机制和某些功能不同(例如,您不能在固定大小的数组上调用
push_back
)。用法如下:
显然这是一个非常粗糙且不完整的示例,但我希望它能够阐明策略背后的概念。
请注意,在示例中,我们可以说
fixed_storage
和dynamic_storage
是StoragePolicy
概念的“模型”。当然,我们需要正式定义StoragePolicy
概念对其模型的要求。在这种情况下,只需定义一个可索引的data
成员变量即可。A concept is a set of requirements on a type. For example, you could have a concept called "RandomAccessible", which places the requirement on a type that it implements
operator[](int)
in O(1) time.As concepts were dropped from the upcoming C++ standard, they only exist intangibly in C++ as documentation. As an example, you could read SGI's description of the Container concept.
When a type meets all the requirements of a concept, you call it a model of that concept. For example,
std::vector
is a model of the Container concept (or, equivalently,std::vector
"models" Container).Finally, a policy is a unit of behaviour, which can be combined with other units of behaviour to build complex classes. For example, say you wanted to build two classes: a fixed-size array, and a dynamically-resizable array. Both these classes have a lot of shared functionality, but just differ in their storage mechanisms, and some of their functionality (e.g. you can't call
push_back
on a fixed-size array).Usage would be as follows:
Obviously this is a very crude and incomplete example, but I hope it illuminates the concept behind a policy.
Note that in the example, we could say that
fixed_storage
anddynamic_storage
are "models" of theStoragePolicy
concept. Of course, we would need to formally define exactly what theStoragePolicy
concepts requires of its models. In this case, it would simply be to define an indexabledata
member variable.SGI 文档将“模型”称为 C++0x 提案中作为“概念”引入的内容:它是编译时,相当于 OO 建模中的“接口”。它总结了通用代码对模板参数提出的要求。
例如,您可以说
std::transform
函数的OutputIterator
参数应该实现operator++()
和operator=( T )
以便该功能正常工作。策略是另一回事:它使算法可以从外部更改。一个很好的例子是 stl 容器中较少使用的 Allocator 参数:它告诉算法如何分配内存。如果愿意,可以创建一个
std::vector
,其中整个vector
函数将在云端而不是在云端分配内存。堆。 (请注意,我不太容易实现这个分配器)。A.o. the SGI documentation refers to 'model' as to what was introduced as 'concepts' in a C++0x proposal: it's the compile-time equivalent to what an 'interface' is in OO modelling. It summarizes the requirements generic code poses on a template parameter.
As an example you can say that the
OutputIterator
parameters of thestd::transform
function should implementoperator++()
andoperator=( T )
in order for the function to work.The policy is another thing: it makes an algorithm changeable from the outside. A nice example is the less used
Allocator
parameter of the stl containers: it tells the algorithm how to allocate memory. If one wants to, one can make astd::vector<int, AllocateOnCloud>
, where the whole of thevector
functions would allocate memory in the cloud instead of on the heap. (I'm not apt to implement this allocator, mind).概念是类型必须满足的一组要求,以便对概念进行建模。
例如,如果对于类型为
的一对对象
表达式a
和b
,则类型T
为LessThanComparable
Ta < b
格式良好,可转换为bool
并产生严格的弱排序关系。int
类型是LessThanComparable
模型的示例。概念可以形成细化层次结构。如果
A
的需求是B
的需求的超集,则概念A
是概念B
的细化。例如,Bi DirectionIterator
是 < a href="http://www.sgi.com/tech/stl/ForwardIterator.html" rel="nofollow noreferrer">ForwardIterator
。概念用于限制模板可以专用的类型集。例如,
std::sort
算法可以接受一对对象,只要它们建模RandomAccessIterator
。请注意,概念是 C++ 标准和各种其他文档中使用的非正式对象。该语言不直接支持概念(还)。
A concept is a set of requirements that a type must satisfy in order to model the concept.
For example, a type
T
isLessThanComparable
if for a pair of objectsa
andb
of typeT
the expressiona < b
is well-formed, convertible tobool
and induces a strict weak ordering relation. The typeint
is an example of a model ofLessThanComparable
.Concepts can form refinement hierarchies. Concept
A
is a refinement of conceptB
if the requirements ofA
are a superset of the requirements ofB
. For example,BidirectionalIterator
is a refinement ofForwardIterator
.Concepts are used to restrict the set of types a template can be specialized with. For example, the
std::sort
algorithm can accept a pair of objects as long as they modelRandomAccessIterator
.Note that concepts are informal objects used in the C++ standard and various other documents. The language does not support concepts directly (yet).