The rule of five stipulates that any constructible, copyable, and movable type that manages a resource should have the following five member functions declared:
The fourth and fifth parts can be combined to one by-value assignment operator to free the left hand side and transfer ownership of the right hand one.
class Vec {
std::unique_ptr<int[]> uptr_;
int size_;
public:
~Vec() = default; // 1. Destroy
(const Vec& rhs) { // 2. Copy constructor
Vecusing std::copy;
uptr_ = std::make_unique<int[]>(rhs.size_);
size_ = rhs.size_;
(rhs.ptr_, rhs.ptr + size_, ptr_);
copy}
(vec&& rhs) noexcept = default; // 3. Move constructor
Vecfriend void swap(Vec& a, Vec& b) noexcept {
.swap(b);
a}
& operator=(Vec copy) { // 4. by value assignment operator
Vec.swap(*this);
copyreturn *this;
}
void swap(Vec& rhs) noexcept {
using std::swap;
(uptr_, rhs.uptr_);
swap(size_, rhs.size_);
swap}
};