什么样的“特质”?在 C++0x 标准中使用/定义
C++ 中的特征封装了一系列操作,允许算法或数据结构使用实例化的类型进行操作。 char_traits
是对字符串
和文件所需函数进行分组的示例。
但并非所有特质的名字中都有“特质”,对吗?我想到了numeric_limits
。这也是一种“特质”吗?即使没有“特质”这个名字?
那么,还有其他模板可以/应该被视为“特征”吗?除了我发现的示例之外:
allocator_traits
如何获取内存pointer_traits
如何间接访问对象- 元编程
type_traits
符号序列的char_taits
iterator_traits
如何前进、后退和到达元素regex_traits
用于...正则表达式。
我想,我也想问的是,特质是否有一个纯粹的定义?
我特别不确定的一些事情是:
- 上面提到的
numeric_limits
的自定义“traits”,[20.11.4],即duration_values
- 哈希怎么样?函子
hash<>
可以被视为特征吗? - 如果是这样的话,不是所有的需求都是“特征”,比如“CopyAssignable”等吗?
- 那么,被抛弃的“概念”是否就是最终的“特质”——定义呢?
更新:到底什么使特质成为特质这个问题在细节上似乎有点争议。也许可以回答另一个问题:是否有一个全面的列表,哪些类似特征的类对于 C++0x 来说是新的,哪些已经在 C++03 中出现了?也许有人知道某个地方的链接?
A trait in C++ encapsulates a family of operations that allow an Algorithm or Data Structure to operator with that type with which it is instantiated. char_traits
are an example for grouping string
- and file-required functions.
But not all traits have "trait" in their name, right? numeric_limits
comes to mind. Is this a "Trait", too? Even without the name "trait" in it?
So, are there other Templates that could/should be considered a "Trait"? Besides the examples I found:
allocator_traits
how to get memorypointer_traits
how to access an object indirectlytype_traits
meta programmingchar_taits
for sequence of symbolsiterator_traits
how to get forward, backward and to the elementregex_traits
for... regexes.
I guess, what I am asking, too, is there a pure definition for traits?
Some things I am especially unsure about are:
numeric_limits
mentioned above<chrono>
s customization "traits", [20.11.4], i.e.duration_values
- what about Hashing? Can the functor
hash<>
be considered to be a trait? - If thats the case, are not all requirements "traits", like "CopyAssignable", etc?
- And then, are the abandoned "Concepts" the ultimate "trait"-Definition?
Update: The question what exactly makes a trait a trait seems a bit controversy in the details. Maybe a another question could be answered: Is there a comprehensive list which of the trait-like classes are new to C++0x, and which ones have already been in C++03? Maybe someone knows of a link to somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是按标准划分的特征的尝试列表。我很容易忽视一些。
新的 C++11 特征:
C++98/03 特征:
Here is an attempted list of the traits divided by standard. I could quite easily be overlooking some.
new C++11 traits:
C++98/03 traits:
对于其他人,我无法发表评论,但如有疑问:
<块引用>
将特质视为一个小物体,其主要目的是承载
另一个对象或算法使用的信息来确定“策略”
或“实施细节”。 - Bjarne Stroustrup
更新:只是为霍华德提供的广泛列表做出我的小小的贡献:
我错误地认为类型特征和<作为 TR1 一部分的正则表达式特征在技术上并不是 C++0x 中新特征群的一部分(尽管即将推出的新标准已大大扩展了类型特征)。
请参阅霍华德对此的评论和澄清。
For the others I cannot comment but when in doubt:
Update: to just make my small contribution to the extensive list Howard provided:
I was wrongly under the impression that the type traits and regex traits beeing part of the TR1 are technically not part of the new traits bunch in C++0x(even though the type traits have been greatly extended by the new upcoming standard).
See Howard's comment and clarification about that.
(类型)特征是泛型编程中的简单元函数。它采用一种类型并返回一组描述该类型某些方面的值、函数和元函数。
这意味着特征是 C++ 类模板。例如,诸如 std::forward_iterator_tag 之类的迭代器基类就不是特征。
注释 -
特征中的某些值本质上可能是布尔值。由于 C++ 模板限制,特征值不能是浮点类型。但是,特征还可以包含函数,并且这些函数对返回类型没有限制。
纯特征类仅包含静态成员;很简单,没有相关的实例数据。因此,它们也不包含构造函数。这种“纯粹”的区别允许我们将像 std::vector这样的类描述为非纯特征类:实际上,它们是它们自己的特征类。
A (type) trait is a simple meta-function in generic programming. It takes one type and returns a set of values, functions and meta-functions describing some aspects of that type.
That means that a trait is a C++ class template. The iterator base classes such as
std::forward_iterator_tag
aren't traits, for instance.Notes -
Some of the values in a trait may be boolean in nature. Due to C++ template restrictions, trait values cannot be of floating-point type. However, traits can also contain functions, and those functions have no restrictions on return type.
Pure trait classes contain only static members; there's simple no relevant instance data. For that reason, they don't contain constructors either. This "pure" distinction allows us to describe classes like
std::vector<T>
as non-pure trait classes: they're their own trait classes, in effect.我真正喜欢的与新枚举类类型相关的类型是
underlying_type::type,它为您提供枚举类的存储说明符的类型,
在枚举转换和序列化中非常有用。
The one that I realy love that goes in hand with the new enum class types is
underlying_type::type which gives you the type of the storage specifier of the enum class
Very useful in enum conversions and serialization.