Copy-and-Swap Idiom

Learn about the copy-and-swap idiom with safe and unsafe variants.

Before moving on to discuss the copy-and-swap idiom, let’s discuss the exception safety, which is important to understand the copy-and-swap idiom.

Exception safety

An operation can be considered exception-safe if those operations leave the object in a valid state. In short, the exception should not corrupt the program or leak resources. This means that all objects allocated using malloc or new are destroyed at the end, and all the resources are closed (RAII). ...