mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Address items called out in Issue 1424 (#1539)
* Address items called out in Issue 1424 * Fix issues caught by CI * Updates based on PR review
This commit is contained in:
parent
fc32070096
commit
39b9ebdf86
|
@ -4512,6 +4512,7 @@ Other default operations rules:
|
|||
* [C.86: Make `==` symmetric with respect of operand types and `noexcept`](#Rc-eq)
|
||||
* [C.87: Beware of `==` on base classes](#Rc-eq-base)
|
||||
* [C.89: Make a `hash` `noexcept`](#Rc-hash)
|
||||
* [C.90: Rely on constructors and assignment operators, not memset and memcpy](#Rc-memset)
|
||||
|
||||
## <a name="SS-defop"></a>C.defop: Default Operations
|
||||
|
||||
|
@ -6539,6 +6540,37 @@ That tends to work better than "cleverness" for non-specialists.
|
|||
|
||||
* Flag throwing `hash`es.
|
||||
|
||||
### <a name="Rc-memset"></a>C.90: Rely on constructors and assignment operators, not `memset` and `memcpy`
|
||||
|
||||
##### Reason
|
||||
|
||||
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
|
||||
|
||||
struct base
|
||||
{
|
||||
virtual void update() = 0;
|
||||
std::shared_ptr<int> sp;
|
||||
};
|
||||
|
||||
struct derived : public base
|
||||
{
|
||||
void update() override {}
|
||||
};
|
||||
|
||||
void init(derived& a)
|
||||
{
|
||||
memset(&a, 0, sizeof(derived));
|
||||
}
|
||||
|
||||
void copy(derived& a, derived& b)
|
||||
{
|
||||
memcpy(&a, &b, sizeof(derived));
|
||||
}
|
||||
|
||||
|
||||
## <a name="SS-containers"></a>C.con: Containers and other resource handles
|
||||
|
||||
A container is an object holding a sequence of objects of some type; `std::vector` is the archetypical container.
|
||||
|
|
|
@ -301,6 +301,7 @@ md
|
|||
memberinit
|
||||
members'
|
||||
memcmp
|
||||
memcpy
|
||||
memmove
|
||||
memoization
|
||||
memoized
|
||||
|
|
Loading…
Reference in New Issue
Block a user