AK: Destroy original value when assigning to Variant

This commit is contained in:
Daniel Bertalan 2021-07-03 17:12:12 +02:00 committed by Ali Mohammad Pur
parent 515e2d9734
commit 39dd13fd17
2 changed files with 12 additions and 0 deletions

View file

@ -266,6 +266,9 @@ public:
requires(!(IsTriviallyCopyConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
#endif
{
if constexpr (!(IsTriviallyDestructible<Ts> && ...)) {
Helper::delete_(m_index, m_data);
}
m_index = other.m_index;
Helper::copy_(other.m_index, other.m_data, m_data);
return *this;
@ -276,6 +279,9 @@ public:
requires(!(IsTriviallyMoveConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
#endif
{
if constexpr (!(IsTriviallyDestructible<Ts> && ...)) {
Helper::delete_(m_index, m_data);
}
m_index = other.m_index;
Helper::move_(other.m_index, other.m_data, m_data);
return *this;

View file

@ -57,6 +57,12 @@ TEST_CASE(destructor)
Variant<DestructionChecker> test_variant { DestructionChecker { was_destroyed } };
}
EXPECT(was_destroyed);
bool was_destroyed_when_assigned_to = false;
Variant<DestructionChecker, int> original { DestructionChecker { was_destroyed_when_assigned_to } };
Variant<DestructionChecker, int> other { 42 };
original = other;
EXPECT(was_destroyed_when_assigned_to);
}
TEST_CASE(move_moves)