@ -6539,6 +6540,37 @@ That tends to work better than "cleverness" for non-specialists.
* Flag throwing `hash`es.
### <aname="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));
}
## <aname="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.