mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Elaborated on PR #1539
This commit is contained in:
parent
39b9ebdf86
commit
54afca9318
|
@ -6547,7 +6547,7 @@ That tends to work better than "cleverness" for non-specialists.
|
|||
The standard C++ mechanism to construct an instance of a type is to call its constructor. As specified in guideline [C.41](#Rc-complete): a constructor should create a fully initialized object. No additional initialization, such as by `memcpy`, should be required.
|
||||
A type will provide a copy constructor and/or copy assignment operator to appropriately make a copy of the class, preserving the type's invariants. Using memcpy to copy a non-trivially copyable type has undefined behavior. Frequently this results in slicing, or data corruption.
|
||||
|
||||
##### Example, bad
|
||||
##### Example, good
|
||||
|
||||
struct base
|
||||
{
|
||||
|
@ -6560,16 +6560,28 @@ A type will provide a copy constructor and/or copy assignment operator to approp
|
|||
void update() override {}
|
||||
};
|
||||
|
||||
##### Example, bad
|
||||
|
||||
void init(derived& a)
|
||||
{
|
||||
memset(&a, 0, sizeof(derived));
|
||||
}
|
||||
|
||||
This is type-unsafe and overwrites the vtable.
|
||||
|
||||
##### Example, bad
|
||||
|
||||
void copy(derived& a, derived& b)
|
||||
{
|
||||
memcpy(&a, &b, sizeof(derived));
|
||||
}
|
||||
|
||||
This is also type-unsafe and overwrites the vtable.
|
||||
|
||||
##### Enforcement
|
||||
|
||||
- Flag passing a non-trivially-copyable type to `memset` or `memcpy`.
|
||||
|
||||
|
||||
## <a name="SS-containers"></a>C.con: Containers and other resource handles
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user