< cpp‎ | experimental‎ | ranges
Technical specifications
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals 2 TS)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Concepts (concepts TS)
Ranges (ranges TS)
Special mathematical functions (special math TR)
General utilities library
Utility components
Function objects
Metaprogramming and type traits
Tagged pairs and tuples
template< MoveConstructible T, class U = T >

    requires Assignable<T&, U>

constexpr T exchange(T& obj, U&& new_val) noexcept(/* see below */);
(ranges TS)

Replaces the value of obj with new_value and returns the old value of obj, as if by

T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;


obj - object whose value to replace
new_value - the value to assign to obj

Return value

The old value of obj


noexcept specification:  
noexcept(std::is_nothrow_move_constructible<T>::value &&
         std::is_nothrow_assignable<T&, U>::value)


See also

replaces the argument with a new value and returns its previous value
(function template)