This pattern is licensed under the CC0 Public Domain Dedication.
Implement the assignment operator with strong exception safety.
The copy-and-swap idiom identifies that we can implement a classes copy/move assignment operators in terms of its copy/move constructor and achieve strong exception safety.
foo, on lines 7–45, has an implementation similar to the
rule of five, yet its copy and
move assignment operators have been replaced with a single
assignment operator on lines 24–29. This assignment operator takes its
argument by value, making use of the existing copy and move
To implement the assignment operator, we simply need to swap the
*this and the argument,
out of scope at the end of the function, it will destroy any
resources that were originally associated with the current object.
To achieve this, we define a
swap function for our class on
lines 36–41, which itself calls
swap on the class’s members (line 40).
We use a using-declaration on line 38 to allow
swap to be found
via argument-dependent lookup
std::swap — this is not strictly necessary
in our case, because we are only swapping a pointer, but is good
practice in general. Our assignment operator then simply swaps
other on line 26.
The copy-and-swap idiom has inherent strong exception safety
because all allocations (if any) occur when copying into the
other argument, before any changes have been made to
It is generally, however, less optimized than a more custom
implementation of the assignment operators.
Note: We can typically avoid manual memory management and having to write the copy/move constructors, assignment operators, and destructor entirely by using the rule of zero