util: Add Expected::swap()

This commit is contained in:
MarcoFalke 2025-12-11 11:02:33 +01:00
parent fabb47e4e3
commit faa59b3679
No known key found for this signature in database
2 changed files with 11 additions and 0 deletions

View File

@ -122,4 +122,13 @@ BOOST_AUTO_TEST_CASE(unexpected_error_accessors)
BOOST_CHECK_EQUAL(*moved, -2);
}
BOOST_AUTO_TEST_CASE(expected_swap)
{
Expected<const char*, std::unique_ptr<int>> a{Unexpected{std::make_unique<int>(-1)}};
Expected<const char*, std::unique_ptr<int>> b{"good"};
a.swap(b);
BOOST_CHECK_EQUAL(a.value(), "good");
BOOST_CHECK_EQUAL(*b.error(), -1);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -88,6 +88,8 @@ public:
constexpr E& error() & noexcept LIFETIMEBOUND { return *Assert(std::get_if<1>(&m_data)); }
constexpr E&& error() && noexcept LIFETIMEBOUND { return std::move(error()); }
constexpr void swap(Expected& other) noexcept { m_data.swap(other.m_data); }
constexpr T& operator*() & noexcept LIFETIMEBOUND { return value(); }
constexpr const T& operator*() const& noexcept LIFETIMEBOUND { return value(); }
constexpr T&& operator*() && noexcept LIFETIMEBOUND { return std::move(value()); }