BAN: Variant allow copy/move from empty

This commit is contained in:
2026-02-07 18:52:40 +02:00
parent 8fb2270ecf
commit 8794122c2d

View File

@@ -126,6 +126,7 @@ namespace BAN
Variant(Variant&& other) Variant(Variant&& other)
: m_index(other.m_index) : m_index(other.m_index)
{ {
if (other.has_value())
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage); detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
other.clear(); other.clear();
} }
@@ -133,6 +134,7 @@ namespace BAN
Variant(const Variant& other) Variant(const Variant& other)
: m_index(other.m_index) : m_index(other.m_index)
{ {
if (other.has_value())
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage); detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
} }
@@ -157,11 +159,12 @@ namespace BAN
Variant& operator=(Variant&& other) Variant& operator=(Variant&& other)
{ {
if (m_index == other.m_index) if (m_index == other.m_index && m_index != invalid_index())
detail::move_assign<Ts...>(m_index, other.m_storage, m_storage); detail::move_assign<Ts...>(m_index, other.m_storage, m_storage);
else else
{ {
clear(); clear();
if (other.has_value())
detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage); detail::move_construct<Ts...>(other.m_index, other.m_storage, m_storage);
m_index = other.m_index; m_index = other.m_index;
} }
@@ -171,11 +174,12 @@ namespace BAN
Variant& operator=(const Variant& other) Variant& operator=(const Variant& other)
{ {
if (m_index == other.m_index) if (m_index == other.m_index && m_index != invalid_index())
detail::copy_assign<Ts...>(m_index, other.m_storage, m_storage); detail::copy_assign<Ts...>(m_index, other.m_storage, m_storage);
else else
{ {
clear(); clear();
if (other.has_value())
detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage); detail::copy_construct<Ts...>(other.m_index, other.m_storage, m_storage);
m_index = other.m_index; m_index = other.m_index;
} }