8 General utilities library [utilities]

8.2 Utility components [utility]

8.2.2 exchange [utility.exchange]

template <MoveConstructible T, class U=T> requires Assignable<T&, U> constexpr T exchange(T& obj, U&& new_val) noexcept(see below);

Effects: Equivalent to:

T old_val = std::move(obj);
obj = std::forward<U>(new_val);
return old_val;

Remarks: The expression in noexcept is equivalent to:

is_nothrow_move_constructible<T>::value &&
is_nothrow_assignable<T&, U>::value