Fix expected::operator*() const& and && failing to compile (#1538)

ETL_ASSERT_OR_RETURN_VALUE tried to return ETL_NULLPTR from functions
returning references, which cannot be initialized from nullptr. Use
plain ETL_ASSERT instead, matching the other operator*() overloads.

Add regression test for dereferencing const and temporary expected.

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
Roland Reichwein 2026-08-17 12:52:42 +02:00 committed by GitHub
parent 70d64496e6
commit 88b629c121
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -746,7 +746,7 @@ namespace etl
//*******************************************
const value_type& operator*() const ETL_LVALUE_REF_QUALIFIER
{
ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
return etl::get<value_type>(storage);
}
@ -757,7 +757,7 @@ namespace etl
//*******************************************
value_type&& operator*() &&
{
ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
return etl::move(etl::get<value_type>(storage));
}

View File

@ -618,6 +618,20 @@ namespace
etl::expected_invalid);
}
//*************************************************************************
// See https://github.com/ETLCPP/etl/issues/1532
TEST(test_dereference_const_and_rvalue_expected)
{
typedef etl::expected<int, const char*> Expected1532;
Expected1532 e = 42;
const Expected1532& ce = e;
CHECK_EQUAL(42, *e);
CHECK_EQUAL(42, *ce);
CHECK_EQUAL(42, *Expected1532(42));
}
//*************************************************************************
struct value_or_helper
{