expected: add const accessors

This commit is contained in:
Danny Robson 2019-03-05 12:26:03 +11:00
parent b7e883903c
commit df4e75d2ac

View File

@ -31,6 +31,9 @@ namespace cruft {
ErrorT& value (void)& { return m_value; } ErrorT& value (void)& { return m_value; }
ErrorT&& value (void)&& { return std::move (m_value); } ErrorT&& value (void)&& { return std::move (m_value); }
ErrorT const& value (void) const & { return m_value; }
ErrorT const&& value (void) const&& { return std::move (m_value); }
private: private:
ErrorT m_value; ErrorT m_value;
}; };
@ -41,6 +44,12 @@ namespace cruft {
using value_type = ValueT; using value_type = ValueT;
using error_type = ErrorT; using error_type = ErrorT;
expected () = delete;
expected (expected &&) = default;
expected& operator=(expected &&) = default;
expected (expected const&) = default;
expected& operator=(expected const&) = default;
expected (ValueT &&_value) expected (ValueT &&_value)
{ {
::new (&m_store.value) ValueT (std::move (_value)); ::new (&m_store.value) ValueT (std::move (_value));
@ -70,6 +79,14 @@ namespace cruft {
return m_store.value; return m_store.value;
} }
ValueT const&
value (void) const&
{
if (!m_valid)
throw bad_expected_access {};
return m_store.value;
}
ValueT&& ValueT&&
value (void)&& value (void)&&
{ {
@ -86,6 +103,13 @@ namespace cruft {
return m_store.error.value (); return m_store.error.value ();
} }
ErrorT const&
error (void) const&
{
if (m_valid) throw bad_expected_access {};
return m_store.error.value ();
}
ErrorT&& ErrorT&&
error (void)&& error (void)&&
{ {