diff --git a/include/etl/expected.h b/include/etl/expected.h index 3ae22937..f7a123f4 100644 --- a/include/etl/expected.h +++ b/include/etl/expected.h @@ -57,12 +57,7 @@ namespace etl //*************************************************************************** /// expected_invalid //*************************************************************************** - template - class expected_invalid; - - //******************************************* - template<> - class expected_invalid : public etl::expected_exception + class expected_invalid : public etl::expected_exception { public: @@ -72,18 +67,6 @@ namespace etl } }; - //******************************************* - template - class expected_invalid : etl::expected_invalid - { - public: - - expected_invalid(string_type file_name_, numeric_type line_number_) - : expected_invalid(file_name_, line_number_) - { - } - }; - //*************************************************************************** /// Unexpected type. /// etl::unexpected represents an unexpected value stored in etl::expected. @@ -668,7 +651,7 @@ namespace etl value_type* operator ->() { #if ETL_IS_DEBUG_BUILD - ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid)); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); #endif return etl::addressof(etl::get(storage)); @@ -680,7 +663,7 @@ namespace etl const value_type* operator ->() const { #if ETL_IS_DEBUG_BUILD - ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid)); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); #endif return etl::addressof(etl::get(storage)); @@ -692,7 +675,7 @@ namespace etl value_type& operator *() ETL_LVALUE_REF_QUALIFIER { #if ETL_IS_DEBUG_BUILD - ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid)); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); #endif return etl::get(storage); @@ -704,7 +687,7 @@ namespace etl const value_type& operator *() const ETL_LVALUE_REF_QUALIFIER { #if ETL_IS_DEBUG_BUILD - ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid)); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); #endif return etl::get(storage); @@ -717,7 +700,7 @@ namespace etl value_type&& operator *()&& { #if ETL_IS_DEBUG_BUILD - ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid)); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); #endif return etl::move(etl::get(storage)); @@ -729,7 +712,7 @@ namespace etl const value_type&& operator *() const&& { #if ETL_IS_DEBUG_BUILD - ETL_ASSERT(storage.index() == Value_Type, ETL_ERROR(expected_invalid)); + ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid)); #endif return etl::move(etl::get(storage)); diff --git a/test/test_expected.cpp b/test/test_expected.cpp index 32bc3c01..941be390 100644 --- a/test/test_expected.cpp +++ b/test/test_expected.cpp @@ -483,6 +483,28 @@ namespace CHECK_EQUAL("error 1", output2.e); } + //************************************************************************* + TEST(test_dereference_operators) + { + struct ExpectedType + { + ExpectedType(int i_) + : i(i_) + { + } + + int i; + }; + + etl::expected exp = etl::unexpected(0); + const etl::expected cexp = etl::unexpected(0); + + CHECK_THROW({ int i = (*exp).i; (void)i; }, etl::expected_invalid); + CHECK_THROW({ int i = (*cexp).i; (void)i; }, etl::expected_invalid); + CHECK_THROW({ int i = exp->i; (void)i; }, etl::expected_invalid); + CHECK_THROW({ int i = cexp->i; (void)i; }, etl::expected_invalid); + } + //************************************************************************* struct value_or_helper { @@ -519,5 +541,28 @@ namespace test_exp = etl::unexpected(2); CHECK_FALSE(test_exp.has_value()); } + + //************************************************************************* + TEST(test_expected_does_not_compile_with_ETL_LOG_ERRORS_bug_787) + { + etl::expected test_exp = etl::unexpected(0); + bool thrown = false; + std::string thrown_what; + std::string exception_what = etl::expected_invalid(__FILE__, __LINE__).what(); + + try + { + int i = *test_exp; + (void)i; + } + catch (etl::exception& e) + { + thrown = true; + thrown_what = e.what(); // what() should be accessible + } + + CHECK_TRUE(thrown); + CHECK_TRUE(exception_what == thrown_what); + } }; }