diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-12-25 10:59:06 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-12-25 10:59:06 +0000 |
| commit | 33bde3e067fca26782fe48edfc90f34c12cf7e69 (patch) | |
| tree | 5e83631beb23d1aad5b9e6a0e788a3bd4256bce5 /src | |
| parent | 2e1c6eaff77f42c91ac57a1db2a700cb4fda9996 (diff) | |
Minor code tweak in optional.hh
Diffstat (limited to 'src')
| -rw-r--r-- | src/optional.hh | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/src/optional.hh b/src/optional.hh index a0406f4f..60b42fad 100644 --- a/src/optional.hh +++ b/src/optional.hh @@ -20,7 +20,7 @@ public: } Optional(Optional&& other) - noexcept(noexcept(new ((void*)0) T(std::move(other.m_value)))) + noexcept(noexcept(new (nullptr) T(std::move(other.m_value)))) : m_valid(other.m_valid) { if (m_valid) @@ -29,8 +29,7 @@ public: Optional& operator=(const Optional& other) { - if (m_valid) - m_value.~T(); + destruct_ifn(); if ((m_valid = other.m_valid)) new (&m_value) T(other.m_value); return *this; @@ -38,18 +37,13 @@ public: Optional& operator=(Optional&& other) { - if (m_valid) - m_value.~T(); + destruct_ifn(); if ((m_valid = other.m_valid)) new (&m_value) T(std::move(other.m_value)); return *this; } - ~Optional() - { - if (m_valid) - m_value.~T(); - } + ~Optional() { destruct_ifn(); } constexpr explicit operator bool() const noexcept { return m_valid; } @@ -79,6 +73,8 @@ public: const T* operator->() const { return const_cast<Optional&>(*this).operator->(); } private: + void destruct_ifn() { if (m_valid) m_value.~T(); } + bool m_valid; union { T m_value; }; }; |
