什么样的“特质”?在 C++0x 标准中使用/定义

发布于 2024-11-24 14:02:29 字数 1046 浏览 5 评论 0原文

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 memory
  • pointer_traits how to access an object indirectly
  • type_traits meta programming
  • char_taits for sequence of symbols
  • iterator_traits how to get forward, backward and to the element
  • regex_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我最亲爱的 2024-12-01 14:02:30

这是按标准划分的特征的尝试列表。我很容易忽视一些。

新的 C++11 特征:

is_error_code_enum
is_error_condition_enum
pointer_traits
allocator_traits
Just about everything in <type_traits>
treat_as_floating_point
duration_values
uses_allocator
regex_traits

C++98/03 特征:

numeric_limits
char_traits
iterator_traits

Here is an attempted list of the traits divided by standard. I could quite easily be overlooking some.

new C++11 traits:

is_error_code_enum
is_error_condition_enum
pointer_traits
allocator_traits
Just about everything in <type_traits>
treat_as_floating_point
duration_values
uses_allocator
regex_traits

C++98/03 traits:

numeric_limits
char_traits
iterator_traits
献世佛 2024-12-01 14:02:30
  • *numeric_limits* 绝对代表数字类型的一组特征。
  • 像“CopyAssignable”等所有要求确实是特征,请参阅 这篇关于特质的论文

    对于其他人,我无法发表评论,但如有疑问:

    <块引用>

    将特质视为一个小物体,其主要目的是承载
    另一个对象或算法使用的信息来确定“策略”
    或“实施细节”。 - Bjarne Stroustrup

    更新:只是为霍华德提供的广泛列表做出我的小小的贡献:

  • 时间相关的特征
  • 正则表达式特征

我错误地认为类型特征和<作为 TR1 一部分的正则表达式特征在技术上并不是 C++0x 中新特征群的一部分(尽管即将推出的新标准已大大扩展了类型特征)。
请参阅霍华德对此的评论和澄清。

  • *numeric_limits* definitely represents a set of traits for the numeric types.
  • all requirements like "CopyAssignable" etc. are indeed traits see this paper on traits

    For the others I cannot comment but when in doubt:

    Think of a trait as a small object whose main purpose is to carry
    information used by another object or algorithm to determine "policy"
    or "implementation details". - Bjarne Stroustrup

    Update: to just make my small contribution to the extensive list Howard provided:

  • time-related traites
  • regex traits

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.

吖咩 2024-12-01 14:02:30

(类型)特征是泛型编程中的简单元函数。它采用一种类型并返回一组描述该类型某些方面的值、函数和元函数。

这意味着特征是 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.

谜兔 2024-12-01 14:02:30

我真正喜欢的与新枚举类类型相关的类型是

underlying_type::type,它为您提供枚举类的存储说明符的类型,

enum class My_Enum : unsigned int { ... }

underlying_type<My_Enum>::type -> unsigned int

在枚举转换和序列化中非常有用。

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

enum class My_Enum : unsigned int { ... }

underlying_type<My_Enum>::type -> unsigned int

Very useful in enum conversions and serialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文