Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/TriviallyCopyable"

From cppreference.com
< cpp‎ | named req
m (s/collvectively/collectively)
m (+C++11 in title)
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/named req/title|TriviallyCopyable}}
+
{{cpp/named req/title|TriviallyCopyable|notes={{mark since c++11}}}}
 
{{cpp/named req/navbar}}
 
{{cpp/named req/navbar}}
 +
 +
{{cpp/named req/core cat note}}
  
 
===Requirements===
 
===Requirements===
Following type are collectively called ''trivially copyable types'':
+
The following types are collectively called ''trivially copyable types'':
 +
* {{rlp|ScalarType|scalar types}}
 +
* [[cpp/language/classes#Trivially copyable class|trivially copyable class types]]
 +
* arrays of such types
 +
* cv-qualified versions of these types
 +
 
 +
===Notes===
 +
In general, for any trivially copyable type {{tt|T}} and an object {{tt|obj1}} of {{tt|T}}, the underlying bytes of {{tt|obj1}} can be copied into an array of {{c|char}}, or {{c|unsigned char}}{{rev inl|since=c++17|, or {{ltt|cpp/types/byte|std::byte}}}} or into {{tt|obj2}}, a distinct object of {{tt|T}}. Neither {{tt|obj1}} nor {{tt|obj2}} may be a potentially-overlapping subobject.
  
*Scalar types<!-- CWG 2094 reverted CWG 1746 -->
+
If the underlying bytes of {{tt|obj1}} are copied into such an array, and then the resulting content is copied back into {{tt|obj1}}, {{tt|obj1}} will hold its original value. If the underlying bytes of {{tt|obj1}} are copied into {{tt|obj2}}, {{tt|obj2}} will hold {{tt|obj1}}'s value.
*Trivially copyable classes, i.e. classes satisfying following requirements:
+
**Every copy constructor is [[cpp/language/copy_constructor#Trivial_copy_constructor|trivial]] or deleted <!--CWG 1734-->
+
**Every move constructor is [[cpp/language/move_constructor#Trivial_move_constructor|trivial]] or deleted <!--CWG 1734-->
+
**Every copy assignment operator is [[cpp/language/copy assignment#Trivial_copy_assignment_operator|trivial]] or deleted <!--CWG 1734-->
+
**Every move assignment operator is [[cpp/language/move assignment#Trivial_move_assignment_operator|trivial]] or deleted <!--CWG 1734-->
+
**At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is non-deleted <!--CWG 1734-->
+
**Has a [[cpp/language/destructor#Trivial_destructor|trivial]] non-deleted destructor
+
* Arrays of {{named req/core|TriviallyCopyable}} objects
+
  
This implies that a trivially copyable class has no [[cpp/language/virtual|virtual functions]] or [[cpp/language/derived_class#Virtual_base_classes|virtual base classes]].
+
Underlying bytes can be copied by {{lc|std::memcpy}} or {{lc|std::memmove}}, as long as no living volatile object is accessed.
  
 
===Defect reports===
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list begin}}
 
{{dr list item|wg=cwg|dr=1734|std=C++11|before=C++03 POD with deleted non-trivial assignment was not trivial|after=deleted ctors/operators allowed}}
 
{{dr list item|wg=cwg|dr=1734|std=C++11|before=C++03 POD with deleted non-trivial assignment was not trivial|after=deleted ctors/operators allowed}}
{{dr list item|wg=cwg|dr=2094|std=C++11|before=Volatile scalar types are not trivially copyable (CWG 1746)|after=made trivially copyable}}
+
{{dr list item|wg=cwg|dr=2094|std=C++11|before=Volatile scalar types are not trivially copyable ({{cwg|1746|-}})|after=made trivially copyable}}
 
{{dr list end}}
 
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/types/dsc is_trivially_copyable}}
+
{{dsc inc|cpp/types/dsc is_trivially_copyable}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 12:06, 8 November 2022

 
 
C++ named requirements
 

Note: the standard doesn't define a named requirement with this name. This is a type category defined by the core language. It is included here as a named requirement only for consistency.

Contents

[edit] Requirements

The following types are collectively called trivially copyable types:

[edit] Notes

In general, for any trivially copyable type T and an object obj1 of T, the underlying bytes of obj1 can be copied into an array of char, or unsigned char, or std::byte(since C++17) or into obj2, a distinct object of T. Neither obj1 nor obj2 may be a potentially-overlapping subobject.

If the underlying bytes of obj1 are copied into such an array, and then the resulting content is copied back into obj1, obj1 will hold its original value. If the underlying bytes of obj1 are copied into obj2, obj2 will hold obj1's value.

Underlying bytes can be copied by std::memcpy or std::memmove, as long as no living volatile object is accessed.

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1734 C++11 C++03 POD with deleted non-trivial assignment was not trivial deleted ctors/operators allowed
CWG 2094 C++11 Volatile scalar types are not trivially copyable (CWG 1746) made trivially copyable

[edit] See also

checks if a type is trivially copyable
(class template) [edit]