Fix value_or not taking const in optional (#1225)

This commit is contained in:
Helder Duarte 2025-11-27 08:48:27 +00:00 committed by GitHub
parent e5dd953c8b
commit 78f04ad298
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View File

@ -471,7 +471,7 @@ namespace etl
etl::enable_if_t<etl::is_convertible<U, T>::value, T>
value_or(U&& default_value) const&
{
return has_value() ? value() : etl::forward<T>(default_value);
return has_value() ? value() : static_cast<T>(etl::forward<U>(default_value));
}
//***************************************************************************
@ -482,7 +482,7 @@ namespace etl
etl::enable_if_t<etl::is_convertible<U, T>::value, T>
value_or(U&& default_value)&&
{
return has_value() ? etl::move(value()) : etl::forward<T>(default_value);
return has_value() ? etl::move(value()) : static_cast<T>(etl::forward<U>(default_value));
}
#endif
@ -1085,7 +1085,7 @@ namespace etl
etl::enable_if_t<etl::is_convertible<U, T>::value, T>
value_or(U&& default_value) const&
{
return has_value() ? value() : etl::forward<T>(default_value);
return has_value() ? value() : static_cast<T>(etl::forward<U>(default_value));
}
//***************************************************************************
@ -1096,7 +1096,7 @@ namespace etl
etl::enable_if_t<etl::is_convertible<U, T>::value, T>
value_or(U&& default_value)&&
{
return has_value() ? etl::move(value()) : etl::forward<T>(default_value);
return has_value() ? etl::move(value()) : static_cast<T>(etl::forward<U>(default_value));
}
#endif

View File

@ -298,6 +298,21 @@ namespace
CHECK_EQUAL(Data("Value"), result);
}
//*************************************************************************
TEST(test_value_or_const)
{
using FundamentalType = int;
using NonFundamentalType = std::string;
const FundamentalType constFT{ 5 };
int resultFT = etl::optional<FundamentalType>{}.value_or(constFT);
CHECK_EQUAL(5, resultFT);
const NonFundamentalType constNFT{ "Default" };
NonFundamentalType resultNFT = etl::optional<NonFundamentalType>{}.value_or(constNFT);
CHECK_EQUAL("Default", resultNFT);
}
//*************************************************************************
struct github_bug_720_bug_helper
{