expected: add a trivial move constructor

This commit is contained in:
Danny Robson 2019-03-19 17:08:35 +11:00
parent fdcab4eafd
commit 66da52fdeb

View File

@ -46,7 +46,18 @@ namespace cruft {
using error_type = ErrorT; using error_type = ErrorT;
expected () = delete; expected () = delete;
expected (expected &&); expected (expected &&rhs)
{
destroy ();
if (rhs) {
m_valid = true;
::new (&m_store.value) ValueT (std::move (rhs.value ()));
} else {
m_valid = false;
::new (&m_store.error) unexpected<ErrorT> (std::move (rhs.error ()));
}
}
expected& operator=(expected &&); expected& operator=(expected &&);
expected (expected const&); expected (expected const&);
expected& operator=(expected const&); expected& operator=(expected const&);
@ -71,12 +82,7 @@ namespace cruft {
~expected () ~expected ()
{ {
if (m_valid) { destroy ();
m_store.value.~ValueT ();
m_valid = false;
} else {
m_store.error.~unexpected<ErrorT> ();
}
} }
ValueT& ValueT&
@ -137,6 +143,16 @@ namespace cruft {
explicit operator bool() const noexcept { return has_value (); } explicit operator bool() const noexcept { return has_value (); }
private: private:
void destroy (void)
{
if (m_valid) {
m_store.value.~ValueT ();
m_valid = false;
} else {
m_store.error.~unexpected<ErrorT> ();
}
}
bool m_valid = false; bool m_valid = false;
union storage_t { union storage_t {