mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
AK: Destroy original value when assigning to Variant
This commit is contained in:
parent
515e2d9734
commit
39dd13fd17
Notes:
sideshowbarker
2024-07-18 10:31:06 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/39dd13fd17f Pull-request: https://github.com/SerenityOS/serenity/pull/8391 Reviewed-by: https://github.com/alimpfard
2 changed files with 12 additions and 0 deletions
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue