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;
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 (expected const&);
expected& operator=(expected const&);
@ -71,12 +82,7 @@ namespace cruft {
~expected ()
{
if (m_valid) {
m_store.value.~ValueT ();
m_valid = false;
} else {
m_store.error.~unexpected<ErrorT> ();
}
destroy ();
}
ValueT&
@ -137,6 +143,16 @@ namespace cruft {
explicit operator bool() const noexcept { return has_value (); }
private:
void destroy (void)
{
if (m_valid) {
m_store.value.~ValueT ();
m_valid = false;
} else {
m_store.error.~unexpected<ErrorT> ();
}
}
bool m_valid = false;
union storage_t {