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
hash<>
be considered to be a trait?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?
What are traits in C/C++? Traits allow us to learn more information about a type. Using traits, an algorithm can change how it works depending on specific object traits, such as functions and variables. Traits are written in the form of a templated struct.
Traits classes do not determine the type of the object. Instead, they provide additional information about a type, typically by defining typedefs or constants inside the trait.
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
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With