121
Deep and shallow copying is the simplest example. The gist of the problem is that
by copying the data manually, we may break the inherent behavior of the object which
may rely on the copy constructor. Any shared or unique pointer would be another great
example – by simple copying it with memcpy(), we break the "deal" we made with that
class when we used it, and it may lead to leaks or crashes.
The application must handle many more sophisticated details when it manually
copies the contents of an object. The C++11 standard provides a
type trait and std::is_trivially_copyable, which ensure a given type satisfies the
requirements of TriviallyCopyable. Referring to C++ standard, an object satisfies the
TriviallyCopyable requirements when
A trivially copyable class is a class that:
— has no non-trivial copy constructors (12.8),
— has no non-trivial move constructors (12.8),
— has no non-trivial copy assignment operators (13.5.3, 12.8),
— has no non-trivial move assignment operators (13.5.3, 12.8), and
— has a trivial destructor (12.4).
A trivial class is a class that has a trivial default constructor (12.1) and is
trivially copyable.
[Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes.]
The C++ standard defines nontrivial methods as follows:
A copy/move constructor for class X is trivial if it is not user-provided and if
— class X has no virtual functions (10.3) and no virtual base classes (10.1),
and
— the constructor selected to copy/move each direct base class subobject is
trivial, and
— for each non-static data member of X that is of class type (or array
thereof ), the constructor selected to copy/move that member is trivial;
otherwise, the copy/move constructor is non-trivial.
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
Précédent

- 147/457

Suivant