std::exchange use cases
What and Why
这次单独说一下 std::exchange,它是 C++14 <utility> 提供的一个函数模板,实现很简单。
template<class T, class U = T>
constexpr // since C++20
T exchange(T& obj, U&& new_value)
noexcept( // since C++23
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_assignable<T&, U>::value
)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);… Continue Reading