Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 355
towi Avatar asked Jul 16 '11 16:07

towi


People also ask

What are traits in C?

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.

What are traits classes C++?

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.


2 Answers

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 
like image 125
Howard Hinnant Avatar answered Oct 11 '22 05:10

Howard Hinnant


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

like image 35
celavek Avatar answered Oct 11 '22 05:10

celavek