util: Make Expected::value() throw
This is not expected to be needed in this codebase, but brings the implementation closer to std::expected::value(). Also, add noexcept, where std::expected has them. This will make operator-> and operator* terminate, when has_value() is false.
This commit is contained in:
2 files changed
+24
-6
No files matched your search
@@ -53,6 +53,15 @@ BOOST_AUTO_TEST_CASE(expected_value_or)
|
|||||||
BOOST_CHECK_EQUAL(const_val.value_or("fallback"), "fallback");
|
BOOST_CHECK_EQUAL(const_val.value_or("fallback"), "fallback");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(expected_value_throws)
|
||||||
|
{
|
||||||
|
const Expected<int, std::string> e{Unexpected{"fail"}};
|
||||||
|
BOOST_CHECK_THROW(e.value(), BadExpectedAccess);
|
||||||
|
|
||||||
|
const Expected<void, std::string> void_e{Unexpected{"fail"}};
|
||||||
|
BOOST_CHECK_THROW(void_e.value(), BadExpectedAccess);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(expected_error)
|
BOOST_AUTO_TEST_CASE(expected_error)
|
||||||
{
|
{
|
||||||
Expected<void, std::string> e{};
|
Expected<void, std::string> e{};
|
||||||
|
|||||||
+15
-6
@@ -8,6 +8,7 @@
|
|||||||
#include <attributes.h>
|
#include <attributes.h>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <exception>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
@@ -30,6 +31,10 @@ private:
|
|||||||
E m_error;
|
E m_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BadExpectedAccess : std::exception {
|
||||||
|
const char* what() const noexcept override { return "Bad util::Expected access"; }
|
||||||
|
};
|
||||||
|
|
||||||
/// The util::Expected class provides a standard way for low-level functions to
|
/// The util::Expected class provides a standard way for low-level functions to
|
||||||
/// return either error values or result values.
|
/// return either error values or result values.
|
||||||
///
|
///
|
||||||
@@ -55,12 +60,16 @@ public:
|
|||||||
|
|
||||||
constexpr const ValueType& value() const LIFETIMEBOUND
|
constexpr const ValueType& value() const LIFETIMEBOUND
|
||||||
{
|
{
|
||||||
assert(has_value());
|
if (!has_value()) {
|
||||||
|
throw BadExpectedAccess{};
|
||||||
|
}
|
||||||
return std::get<0>(m_data);
|
return std::get<0>(m_data);
|
||||||
}
|
}
|
||||||
constexpr ValueType& value() LIFETIMEBOUND
|
constexpr ValueType& value() LIFETIMEBOUND
|
||||||
{
|
{
|
||||||
assert(has_value());
|
if (!has_value()) {
|
||||||
|
throw BadExpectedAccess{};
|
||||||
|
}
|
||||||
return std::get<0>(m_data);
|
return std::get<0>(m_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,11 +95,11 @@ public:
|
|||||||
return std::get<1>(m_data);
|
return std::get<1>(m_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr ValueType& operator*() LIFETIMEBOUND { return value(); }
|
constexpr ValueType& operator*() noexcept LIFETIMEBOUND { return value(); }
|
||||||
constexpr const ValueType& operator*() const LIFETIMEBOUND { return value(); }
|
constexpr const ValueType& operator*() const noexcept LIFETIMEBOUND { return value(); }
|
||||||
|
|
||||||
constexpr ValueType* operator->() LIFETIMEBOUND { return &value(); }
|
constexpr ValueType* operator->() noexcept LIFETIMEBOUND { return &value(); }
|
||||||
constexpr const ValueType* operator->() const LIFETIMEBOUND { return &value(); }
|
constexpr const ValueType* operator->() const noexcept LIFETIMEBOUND { return &value(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
|
|||||||
Reference in new issue
Block a user