From ea91dabee5aa0186cedf62c2cfae62b9f9d29008 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 10 Sep 2025 11:37:30 +0200 Subject: [PATCH] Regression fix: Support zero arguments emplace() in etl::optional (#1183) * Added coderabbitai configuration * Added builtin mem function tests * Modified etl::typed_storage * Modified etl::typed_storage # Conflicts: # include/etl/alignment.h * Added ETL_NOEXCEPT and ETL_NOEXCEPT_IF_NO_THROW * Added etl::typed_storage_ext and swap for same * Added etl::typed_storage_ext and swap for same # Conflicts: # include/etl/alignment.h * Added release notes * Fixes to GCC -O2 errors * Changed char* parameters to value_type* parameters * Fixed compilation issues for const containers unit tests * Added automatic selection of __builtin_memxxx functions for GCC and clang * Added enhanced coderabbit configuration * Updated version and release notes * Disabled constexpr const container tests for C++11 * Attempted fixes for MacOS compilation * Attempted fixes for MacOS compilation * Attempted fixes for MacOS compilation * Attempted fixes for MacOS compilation * Updated version and release notes * feat: removed unreachable break statements (#1169) * Updated version and release notes * Modified etl::typed_storage # Conflicts: # include/etl/alignment.h * Support zero arguments emplace() in etl::optional For non-fundamental types, a recent change in etl::optional was introduced that doesn't support zero arguments emplace() anymore. This change fixes it and adds the respective test. --------- Co-authored-by: John Wellbelove Co-authored-by: Drew Rife --- include/etl/optional.h | 11 +++++++++++ test/test_optional.cpp | 16 ++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/etl/optional.h b/include/etl/optional.h index 48289116..e8e40439 100644 --- a/include/etl/optional.h +++ b/include/etl/optional.h @@ -536,6 +536,17 @@ namespace etl return storage.u.value; } + + //************************************************************************* + /// Emplaces with zero arguments, i.e. default construct emplace. + //************************************************************************* + ETL_CONSTEXPR20_STL + T& emplace() + { + storage.construct(); + + return storage.u.value; + } #else //************************************************************************* /// Emplaces a value. diff --git a/test/test_optional.cpp b/test/test_optional.cpp index e9623b87..c5a34d08 100644 --- a/test/test_optional.cpp +++ b/test/test_optional.cpp @@ -218,15 +218,27 @@ namespace } //************************************************************************* - TEST(test_emplace_zero_parameters) + TEST(test_emplace_zero_parameters_fundamental) { etl::optional result = 1; - result.emplace(); + CHECK_EQUAL(0, static_cast(result.emplace())); CHECK_TRUE(result.has_value()); CHECK_EQUAL(0, int(result.value())); } + //************************************************************************* + TEST(test_emplace_zero_parameters_non_fundamental) + { + etl::optional result = std::string("abc"); + + std::string& ref = result.emplace(); + CHECK_EQUAL(std::string(), ref); + CHECK_EQUAL(&ref, &result.value()); + CHECK_TRUE(result.has_value()); + CHECK_EQUAL("", std::string(result.value())); + } + //************************************************************************* TEST(test_emplace_return) {