[utility.exchange]

# 22 General utilities library [[utilities]](./#utilities)

## 22.2 Utility components [[utility]](utility#exchange)

### 22.2.3 exchange [utility.exchange]

[ð](#lib:exchange)

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

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L308)

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

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L317)

*Remarks*: The exception specification is equivalent to:is_nothrow_move_constructible_v<T> && is_nothrow_assignable_v<T&, U>
