From f9e9674bcfdb7c0877b6095affc60a29be0feacf Mon Sep 17 00:00:00 2001 From: Chiraffollo Date: Tue, 22 Nov 2022 08:46:44 +0100 Subject: [PATCH] Fix bug #636 in optional emplace for pod types --- include/etl/optional.h | 6 ------ test/test_optional.cpp | 10 ++++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/etl/optional.h b/include/etl/optional.h index 7ab1d8c21..4707d55b2 100644 --- a/include/etl/optional.h +++ b/include/etl/optional.h @@ -764,12 +764,6 @@ namespace etl template ETL_CONSTEXPR14 void emplace(Args && ... args) { - if (valid) - { - // Destroy the old one. - storage.template get_reference().~T(); - } - storage = T(ETL_OR_STD::forward(args)...); valid = true; } diff --git a/test/test_optional.cpp b/test/test_optional.cpp index 44a91557e..03eee1d12 100644 --- a/test/test_optional.cpp +++ b/test/test_optional.cpp @@ -484,5 +484,15 @@ namespace CHECK_EQUAL(false, result.has_value()); } + + //************************************************************************* + TEST(test_optional_emplace_bug_636) + { + etl::optional result = 1; + result.emplace(2); + + CHECK_TRUE(result.has_value()); + CHECK_EQUAL(2, result.value()); + } }; }