The rule of zero
This pattern is licensed under the CC0 Public Domain Dedication.
Utilise the value semantics of existing types to avoid having to implement custom copy and move operations.
The rule of zero states that we can avoid writing any custom copy/move constructors, assignment operators, or destructors by using existing types that support the appropriate copy/move semantics.
foo on lines 4–9, for example, does not perform any
manual memory management, yet correctly supports copies and
moves without any memory leaks. The defaulted copy/move
constructors and assignment operators will simply copy or move
each member. For the
x (line 7), this will copy its value.
v (line 8), which is a
all of its elements will be copied over.
bar on lines 11–15 is not copyable by default because it
std::unique_ptr member which
itself is not copyable. However, it correctly supports move
operations, which will transfer ownership of the dynamically