问题
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?
回答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