The rationale for the types of features that verify the triviality of the special functions of the class

In addition to std::is_trivial and std::is_trivially_copyable , C ++ 11 provides several types of attributes to check if types have trivial constructors, destructors, and copy / move assignment operators, i.e.

  • std::is_trivially_constructible
  • std::is_trivially_default_constructible
  • std::is_trivially_copy_constructible
  • std::is_trivially_move_constructible
  • std::is_trivially_assignable
  • std::is_trivially_copy_assignable
  • std::is_trivially_move_assignable
  • std::is_trivially_destructible

What is their original purpose? Of course, some documents (documents) of the C ++ committee should explain the rationale for their inclusion in the standard C ++ library.

+5
source share
1 answer

Why are they in the standard library? Because they are useful, but impossible to implement in the language.


Two concrete examples of utility.

  • std::is_trivially_copy_constructible - If I have a vector type that is a construction with a trivial copy, I do not need to individually copy each element when I redistribute. I can memcpy() whole block in one go. We need a trait of this type to check if this optimization is safe.
  • std::is_trivially_destructible - Trivial destruction is an important type quality. This is one of the criteria for it to be a literal type and therefore used in constant expression. There are situations when I want my type to be used as a literal, where possible (for example, std::optional ). We need a trait of this type to make optional<T> conditionally trivially destructible.
+2
source

Source: https://habr.com/ru/post/1264474/


All Articles