Rules of 3
Rules of 5
A user-defined (or
= default or
= delete declared) destructor, copy-constructor, or copy-assignment operator prevents implicit definition of the move constructor and the move assignment operator, any class for which move semantics are desirable, has to declare all five special member functions:
Copy and Swap Idiom
Use copy constructor to create a copy of the old data called
swap the new data and old data. The temporary copy then destructs, so the old data will gone. The new data lives.
In order to use the copy-and-swap idiom, we need three things: a working copy-constructor, a working destructor (both are the basis of any wrapper, so should be complete anyway), and a
A swap function is a non-throwing function that swaps two objects of a class, member for member. We might be tempted to use
std::swap instead of providing our own, but this would be impossible;
std::swap uses the copy-constructor and copy-assignment operator within its implementation, and we’d ultimately be trying to define the assignment operator in terms of itself!