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).

Note: David Abrahams is an author and computer programmer. He is well recognized for his contributions to the C++ programming language. His contributions to the language include the development of an exception theory and participation on the C++ standards committee. ...