Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason why `std::exchange` is not `constexpr`?

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 obj with new_value and returns the old value of obj.

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?

like image 582
Vittorio Romeo Avatar asked Nov 12 '17 11:11

Vittorio Romeo


1 Answers

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.

like image 57
tambre Avatar answered Oct 07 '22 10:10

tambre