Minor code tweak in optional.hh
This commit is contained in:
parent
2e1c6eaff7
commit
33bde3e067
|
@ -20,7 +20,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional(Optional&& other)
|
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)
|
: m_valid(other.m_valid)
|
||||||
{
|
{
|
||||||
if (m_valid)
|
if (m_valid)
|
||||||
|
@ -29,8 +29,7 @@ public:
|
||||||
|
|
||||||
Optional& operator=(const Optional& other)
|
Optional& operator=(const Optional& other)
|
||||||
{
|
{
|
||||||
if (m_valid)
|
destruct_ifn();
|
||||||
m_value.~T();
|
|
||||||
if ((m_valid = other.m_valid))
|
if ((m_valid = other.m_valid))
|
||||||
new (&m_value) T(other.m_value);
|
new (&m_value) T(other.m_value);
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -38,18 +37,13 @@ public:
|
||||||
|
|
||||||
Optional& operator=(Optional&& other)
|
Optional& operator=(Optional&& other)
|
||||||
{
|
{
|
||||||
if (m_valid)
|
destruct_ifn();
|
||||||
m_value.~T();
|
|
||||||
if ((m_valid = other.m_valid))
|
if ((m_valid = other.m_valid))
|
||||||
new (&m_value) T(std::move(other.m_value));
|
new (&m_value) T(std::move(other.m_value));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Optional()
|
~Optional() { destruct_ifn(); }
|
||||||
{
|
|
||||||
if (m_valid)
|
|
||||||
m_value.~T();
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr explicit operator bool() const noexcept { return m_valid; }
|
constexpr explicit operator bool() const noexcept { return m_valid; }
|
||||||
|
|
||||||
|
@ -79,6 +73,8 @@ public:
|
||||||
const T* operator->() const { return const_cast<Optional&>(*this).operator->(); }
|
const T* operator->() const { return const_cast<Optional&>(*this).operator->(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void destruct_ifn() { if (m_valid) m_value.~T(); }
|
||||||
|
|
||||||
bool m_valid;
|
bool m_valid;
|
||||||
union { T m_value; };
|
union { T m_value; };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user