Swapping two objects

Before move semantics were added in C++11, swapping the content of two objects was a common way to transfer data without allocating and copying. The objects simply swaps the content with each other:

auto a = Object{}; 
auto b = Object{}; 
std::swap(a, b); 

The following image illustrates the process:

Swapping resources between two objects

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.