diff --git a/include/etl/expected.h b/include/etl/expected.h index f7a123f4..0c3dd983 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -38,6 +38,7 @@ SOFTWARE. #include "error_handler.h" #include "utility.h" #include "variant.h" +#include "initializer_list.h" namespace etl { @@ -353,6 +354,7 @@ namespace etl { } +#if ETL_HAS_INITIALIZER_LIST //******************************************* /// Construct value type from initializser_list and arguments. //******************************************* @@ -361,6 +363,7 @@ namespace etl : storage(il, etl::forward(args)...) { } +#endif //******************************************* /// Construct error type from arguments. @@ -608,17 +611,23 @@ namespace etl template ETL_CONSTEXPR14 value_type& emplace(Args&&... args) ETL_NOEXCEPT { - storage.emplace(etl::forward(args)...); + storage.template emplace(etl::forward(args)...); + + return value(); } //******************************************* /// //******************************************* +#if ETL_HAS_INITIALIZER_LIST template - ETL_CONSTEXPR14 value_type& emplace(std::initializer_list& il, Args&&... args) ETL_NOEXCEPT + ETL_CONSTEXPR14 value_type& emplace(std::initializer_list il, Args&&... args) ETL_NOEXCEPT { - storage.emplace(il, etl::forward(args)...); + storage.template emplace(il, etl::forward(args)...); + + return value(); } +#endif #else //******************************************* /// diff --git a/test/test_expected.cpp b/test/test_expected.cpp index 941be390..a199707a 100644 --- a/test/test_expected.cpp +++ b/test/test_expected.cpp @@ -32,6 +32,7 @@ SOFTWARE. #include "etl/type_traits.h" #include +#include namespace { @@ -441,6 +442,70 @@ namespace CHECK_EQUAL(input1.e, output2.e); } + //************************************************************************* + TEST(test_emplace_from_initializer_list) + { + struct S + { + S() + : vi() + , a(0) + , b(0) + { + } + + S(std::initializer_list il, int a_, int b_) + : vi(il) + , a(a_) + , b(b_) + { + } + + std::vector vi; + int a; + int b; + }; + + etl::expected exp; + + S s1({ 10, 11, 12 }, 1, 2); + S s2 = exp.emplace({ 10, 11, 12 }, 1, 2); + + CHECK(s1.vi == s2.vi); + CHECK_EQUAL(s1.a, s2.a); + CHECK_EQUAL(s1.b, s2.b); + } + + //************************************************************************* + TEST(test_emplace_from_vargs) + { + struct S + { + S() + : a(0) + , b(0) + { + } + + S(int a_, int b_) + : a(a_) + , b(b_) + { + } + + int a; + int b; + }; + + etl::expected exp; + + S s1(1, 2); + S s2 = exp.emplace(1, 2); + + CHECK_EQUAL(s1.a, s2.a); + CHECK_EQUAL(s1.b, s2.b); + } + //************************************************************************* TEST(test_move_construct_void_value) {