std::exchange
The function std::exchange is a utility in the C++ Standard Library that allows you to replace the value of a variable with a new value while returning the old value. This is particularly useful when you want to update a variable and also need to use its previous value in the same expression.
When you call std::exchange, you provide it with two arguments: the variable you want to change and the new value you want to assign. The function then swaps the old value out and gives it back, making it a convenient tool for managing state changes in your code.