std::is_trivially_copyable

Header: <type_traits>

std::is_trivially_copyable is a UnaryTypeTrait.

# Declarations

template< class T >
struct is_trivially_copyable;

(since C++11)

# Notes

Objects of trivially-copyable types that are not potentially-overlapping subobjects are the only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files with std::ofstream::write() / std::ifstream::read().

# Example

#include <type_traits>
 
struct A { int m; };
static_assert(std::is_trivially_copyable_v<A> == true);
 
struct B { B(B const&) {} };
static_assert(std::is_trivially_copyable_v<B> == false);
 
struct C { virtual void foo(); };
static_assert(std::is_trivially_copyable_v<C> == false);
 
struct D
{
    int m;
 
    D(D const&) = default; // -> trivially copyable
    D(int x) : m(x + 1) {}
};
static_assert(std::is_trivially_copyable_v<D> == true);
 
int main() {}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2015C++11T could be an array of incompleteclass type with unknown boundthe behavior isundefined in this case

# See also