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
withnew_value
and 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?
截至最新的C ++ 20草案,在Albuquerque ISO C ++委员会会议之后 , std::exchange
成为constexpr
,接受了提案P0202R2 。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.