util: Add Unexpected::error()
This is not needed, but a bit closer to the std lib.
This commit is contained in:
2 files changed
+27
-3
No files matched your search
@@ -7,6 +7,11 @@
|
|||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
using namespace util;
|
using namespace util;
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(util_expected_tests)
|
BOOST_AUTO_TEST_SUITE(util_expected_tests)
|
||||||
@@ -65,4 +70,17 @@ BOOST_AUTO_TEST_CASE(expected_error)
|
|||||||
BOOST_CHECK_EQUAL(read.error(), "fail1");
|
BOOST_CHECK_EQUAL(read.error(), "fail1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(unexpected_error_accessors)
|
||||||
|
{
|
||||||
|
Unexpected u{std::make_unique<int>(-1)};
|
||||||
|
BOOST_CHECK_EQUAL(*u.error(), -1);
|
||||||
|
|
||||||
|
*u.error() -= 1;
|
||||||
|
const auto& read{u};
|
||||||
|
BOOST_CHECK_EQUAL(*read.error(), -2);
|
||||||
|
|
||||||
|
const auto moved{std::move(u).error()};
|
||||||
|
BOOST_CHECK_EQUAL(*moved, -2);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
+9
-3
@@ -20,8 +20,14 @@ template <class E>
|
|||||||
class Unexpected
|
class Unexpected
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
constexpr explicit Unexpected(E e) : err(std::move(e)) {}
|
constexpr explicit Unexpected(E e) : m_error(std::move(e)) {}
|
||||||
E err;
|
|
||||||
|
constexpr const E& error() const& noexcept LIFETIMEBOUND { return m_error; }
|
||||||
|
constexpr E& error() & noexcept LIFETIMEBOUND { return m_error; }
|
||||||
|
constexpr E&& error() && noexcept LIFETIMEBOUND { return std::move(m_error); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
E m_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// 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
|
||||||
@@ -40,7 +46,7 @@ public:
|
|||||||
constexpr Expected() : m_data{std::in_place_index_t<0>{}, ValueType{}} {}
|
constexpr Expected() : m_data{std::in_place_index_t<0>{}, ValueType{}} {}
|
||||||
constexpr Expected(ValueType v) : m_data{std::in_place_index_t<0>{}, std::move(v)} {}
|
constexpr Expected(ValueType v) : m_data{std::in_place_index_t<0>{}, std::move(v)} {}
|
||||||
template <class Err>
|
template <class Err>
|
||||||
constexpr Expected(Unexpected<Err> u) : m_data{std::in_place_index_t<1>{}, std::move(u.err)}
|
constexpr Expected(Unexpected<Err> u) : m_data{std::in_place_index_t<1>{}, std::move(u).error()}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user