What kind of “Traits” are used/defined in the C++0x Standard

拟墨画扇 提交于 2021-02-05 20:01:12

问题


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?


回答1:


  • *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.




回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/6718654/what-kind-of-traits-are-used-defined-in-the-c0x-standard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!