`std::exchange` 不是 `constexpr` 有什么原因吗?
Is there any reason why `std::exchange` is not `constexpr`?
std::exchange
,在C++14中引入,指定如下:
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
.
这是来自 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;
}
据我所知,没有什么可以阻止 std::exchange
被标记为 constexpr
。 有没有什么原因让我想念为什么它不能 constexpr
,或者这只是一个疏忽?
截至最新的 C++20 草案,在 the Albuquerque ISO C++ committee meeting, std::exchange
was made constexpr
with the acceptance of the proposal P0202R2 之后。
std::exchange
,在C++14中引入,指定如下:
template< class T, class U = T > T exchange( T& obj, U&& new_value );
Replaces the value of
obj
withnew_value
and returns the old value ofobj
.
这是来自 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;
}
据我所知,没有什么可以阻止 std::exchange
被标记为 constexpr
。 有没有什么原因让我想念为什么它不能 constexpr
,或者这只是一个疏忽?
截至最新的 C++20 草案,在 the Albuquerque ISO C++ committee meeting, std::exchange
was made constexpr
with the acceptance of the proposal P0202R2 之后。