exchange


Motivation

When you want to replace the value of a variable while keeping its old value, you usually have to introduce a temporary variable:


int x = 5;
int new_value = 10;

int old_value = x;
x = new_value;

With exchange, we no longer need this:

#include <utility>

int x = 5;
int old_value = std::exchange(x, 10); 
// x is now 10
// old_value is 5 (the original value of x)