22 General utilities library [utilities]

22.8 Expected objects [expected]

22.8.6 Class template expected [expected.expected] Swap [expected.object.swap]

constexpr void swap(expected& rhs) noexcept(see below);
  • is_­swappable_­v<T> is true and
  • is_­swappable_­v<E> is true and
  • is_­move_­constructible_­v<T> && is_­move_­constructible_­v<E> is true, and
  • is_­nothrow_­move_­constructible_­v<T> || is_­nothrow_­move_­constructible_­v<E> is true.
Effects: See Table 64.
Table 64: swap(expected&) effects [tab:expected.object.swap]
equivalent to: using std​::​swap; swap(val, rhs.val);
calls rhs.swap(*this)
see below
equivalent to: using std​::​swap; swap(unex, rhs.unex);
For the case where rhs.value() is false and this->has_­value() is true, equivalent to: if constexpr (is_nothrow_move_constructible_v<E>) { E tmp(std::move(rhs.unex)); destroy_at(addressof(rhs.unex)); try { construct_at(addressof(rhs.val), std::move(val)); destroy_at(addressof(val)); construct_at(addressof(unex), std::move(tmp)); } catch(...) { construct_at(addressof(rhs.unex), std::move(tmp)); throw; } } else { T tmp(std::move(val)); destroy_at(addressof(val)); try { construct_at(addressof(unex), std::move(rhs.unex)); destroy_at(addressof(rhs.unex)); construct_at(addressof(rhs.val), std::move(tmp)); } catch (...) { construct_at(addressof(val), std::move(tmp)); throw; } } has_val = false; rhs.has_val = true;
Throws: Any exception thrown by the expressions in the Effects.
Remarks: The exception specification is equivalent to: is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T> && is_nothrow_move_constructible_v<E> && is_nothrow_swappable_v<E>
friend constexpr void swap(expected& x, expected& y) noexcept(noexcept(x.swap(y)));
Effects: Equivalent to x.swap(y).