memcpy 是否保留了一个普通对象的有效性?
Does memcpy preserve a trivial object's validity?
如果有一个普通类型的有效对象(在这种情况下,普通类型满足普通 move/copy 可构造的概念),并且 memcpy
将其放入未初始化内存区域, 复制的内存区域是有效对象吗?
根据我读到的假设:一个对象只有在它的构造函数被调用时才有效。
将具有 std::memcpy
的普通类型的对象复制到适当大小和对齐的存储中,将隐式地在该位置开始新对象的生命周期。
有一类名为 implicit-lifetime type 的类型,其要求是:
- a scalar type, or
- an array type, or
- an aggregate class type, or
- a class type that has
- at least one trivial eligible constructor, and
- a trivial, non-deleted destructor,
- or a cv-qualified version of one of above types.
Trivial class types满足这些要求。
implicit-lifetime 类型的对象具有 属性,their lifetime can be started implicitly 是多个函数或操作:
- operations that begin lifetime of an array of type
char
, unsigned char
, or std::byte
, (since C++17) in which case such objects are created in the array,
- call to following allocating functions, in which case such objects are
- created in the allocated storage:
- operator new
- operator new[]
- std::malloc
- std::calloc
- std::realloc
- std::aligned_alloc (since C++17)
- call to following object representation copying functions, in which case such objects are created in the destination region of storage or the result:
- std::memcpy
- std::memmove
- std::bit_cast (since C++20)
如果有一个普通类型的有效对象(在这种情况下,普通类型满足普通 move/copy 可构造的概念),并且 memcpy
将其放入未初始化内存区域, 复制的内存区域是有效对象吗?
根据我读到的假设:一个对象只有在它的构造函数被调用时才有效。
将具有 std::memcpy
的普通类型的对象复制到适当大小和对齐的存储中,将隐式地在该位置开始新对象的生命周期。
有一类名为 implicit-lifetime type 的类型,其要求是:
- a scalar type, or
- an array type, or
- an aggregate class type, or
- a class type that has
- at least one trivial eligible constructor, and
- a trivial, non-deleted destructor,
- or a cv-qualified version of one of above types.
Trivial class types满足这些要求。
implicit-lifetime 类型的对象具有 属性,their lifetime can be started implicitly 是多个函数或操作:
- operations that begin lifetime of an array of type
char
,unsigned char
, orstd::byte
, (since C++17) in which case such objects are created in the array,- call to following allocating functions, in which case such objects are
- created in the allocated storage:
- operator new
- operator new[]
- std::malloc
- std::calloc
- std::realloc
- std::aligned_alloc (since C++17)
- call to following object representation copying functions, in which case such objects are created in the destination region of storage or the result:
- std::memcpy
- std::memmove
- std::bit_cast (since C++20)