expected: add const member accessors

This commit is contained in:
Danny Robson 2019-03-05 23:15:21 +11:00
parent b86a9bbdfb
commit bf734c1f73

View File

@ -66,6 +66,7 @@ namespace cruft {
expected (unexpected<ErrorT> &&_error)
{
::new (&m_store.error) unexpected<ErrorT> (std::move (_error));
m_valid = false;
}
~expected ()
@ -128,20 +129,16 @@ namespace cruft {
ValueT* operator-> (void)& { return &value (); }
ValueT& operator* (void)& { return value (); }
template <typename FunctionT, typename ...ArgsT>
expected then (FunctionT &&func, ArgsT &&...args)
{
if (!m_valid)
return *this;
else
return expected (std::invoke (func, args..., value ()));
}
ValueT const* operator-> (void) const& { return &value (); }
ValueT const& operator* (void) const& { return value (); }
bool has_value (void) const noexcept { return m_valid; }
explicit operator bool() const noexcept { return has_value (); }
private:
bool m_valid = false;
union storage_t {
storage_t () {};
~storage_t () {};
@ -150,7 +147,5 @@ namespace cruft {
ValueT value;
unexpected<ErrorT> error;
} m_store;
bool m_valid = false;
};
}