1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
template<class T, class U>
T exchange(T& old_value, U && new_value)
{
T ret{std::move(old_value)};
old_value = std::forward<U>(new_value);
return ret;
}
template<class T>
struct reset_when_move
{
reset_when_move() = default;
// eventuellement reset_when_move() noexcept : x_{} {}
reset_when_move(reset_when_move const &) = default;
reset_when_move(T x) noexcept : x_(x) {}
reset_when_move(reset_when_move && other) noexcept
: x_(exchange(other.x_, T{}))
{}
reset_when_move & operator = (reset_when_move const &) = default;
reset_when_move & operator = (reset_when_move && other) noexcept
{
x_ = exchange(other.x_, T{});
return *this;
}
operator T & () noexcept { return x_; }
operator T const & () const noexcept { return x_; }
private:
T x_;
}; |
Partager