Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | named req
m (Notes)
(Notes: rev box)
Line 18: Line 18:
  
 
===Notes===
 
===Notes===
Until C++20, a copy constructor, move constructor, copy assignment operator, or move assignment operator is eligible if and only if it is non-deleted.
+
{{rrev|since=c++20|
 +
A copy constructor, move constructor, copy assignment operator, or move assignment operator is eligible if and only if it is non-deleted.
 +
}}
  
 
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.
 
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.

Revision as of 22:27, 15 August 2020

 
 
C++ named requirements
 

Contents

Requirements

The following types are collectively called trivially copyable types:

  • Scalar types
  • Trivially copyable classes, i.e. classes satisfying following requirements:
    • At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is eligible
    • Every eligible copy constructor (if any) is trivial
    • Every eligible move constructor (if any) is trivial
    • Every eligible copy assignment operator (if any) is trivial
    • Every eligible move assignment operator (if any) is trivial
    • Has a trivial non-deleted destructor
  • Arrays of TriviallyCopyable objects

This implies that a trivially copyable class has no virtual functions or virtual base classes.

Notes

A copy constructor, move constructor, copy assignment operator, or move assignment operator is eligible if and only if it is non-deleted.

(since C++20)

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 is 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.

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

See also

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