From 4080980e7e17f7921b96f9bd419151069b4d5f90 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 1 Dec 2024 17:16:32 +0100 Subject: [PATCH] Added return to etl::optional emplace, fixed typo --- include/etl/optional.h | 12 ++++++++---- test/test_optional.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/etl/optional.h b/include/etl/optional.h index 002d010a9..f70b2689c 100644 --- a/include/etl/optional.h +++ b/include/etl/optional.h @@ -484,14 +484,16 @@ namespace etl //************************************************************************* template ETL_CONSTEXPR20_STL - void emplace(TArgs&& ... args) + T& emplace(TArgs&& ... args) { storage.construct(etl::forward(args)...); + + return storage.u.value; } #else //************************************************************************* /// Emplaces a value. - /// 1 parameter. + /// 0 parameters. //************************************************************************* T& emplace() { @@ -1048,14 +1050,16 @@ namespace etl //************************************************************************* template ETL_CONSTEXPR14 - void emplace(TArgs&& ... args) + T& emplace(TArgs&& ... args) { storage.construct(etl::forward(args)...); + + return storage.value; } #else //************************************************************************* /// Emplaces a value. - /// 1 parameter. + /// 0 parameters. //************************************************************************* T& emplace() { diff --git a/test/test_optional.cpp b/test/test_optional.cpp index 61e68672b..87ee24c72 100644 --- a/test/test_optional.cpp +++ b/test/test_optional.cpp @@ -157,6 +157,16 @@ namespace CHECK_EQUAL(0, int(result.value())); } + //************************************************************************* + TEST(test_emplace_return) + { + etl::optional data; + + DataM* datam = &data.emplace(1U); + CHECK_EQUAL(datam, &data.value()); + CHECK(datam != nullptr); + } + //************************************************************************* TEST(test_moveable) {