std::exchange, introduced in C++14, is specified as follows:
template< class T, class U = T > T exchange( T& obj, U&& new_value );Replaces the value of
objwithnew_valueand returns the old value ofobj.
Here's a possible implementation from cppreference:
template<class T, class U = T>
T exchange(T& obj, U&& new_value)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
As far as I can see, there's nothing preventing std::exchange from being marked as constexpr. Is there a reason I am missing why it cannot be constexpr, or is this just an oversight?
As of the latest C++20 draft, after the Albuquerque ISO C++ committee meeting, std::exchange was made constexpr with the acceptance of the proposal P0202R2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With