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) {