diff --git a/include/fixed_containers/enum_utils.hpp b/include/fixed_containers/enum_utils.hpp index 54129614..3fb5203c 100644 --- a/include/fixed_containers/enum_utils.hpp +++ b/include/fixed_containers/enum_utils.hpp @@ -167,8 +167,7 @@ struct RichEnumAdapter }; template -constexpr std::optional> value_of( - const std::string_view& name) +constexpr std::optional value_of(const std::string_view& name) { for (const RichEnum& rich_enum_val : RichEnum::values()) { @@ -182,8 +181,7 @@ constexpr std::optional> value_of( } template -constexpr std::optional> value_of( - const typename RichEnum::BackingEnum& backing_enum) +constexpr std::optional value_of(const typename RichEnum::BackingEnum& backing_enum) { const auto& rich_enum_values = RichEnum::values(); @@ -215,7 +213,7 @@ constexpr std::optional> value_of( } template -constexpr std::optional> value_of( +constexpr std::optional value_of( std::underlying_type_t enum_integer) { return value_of(typename RichEnum::BackingEnum(enum_integer)); @@ -420,19 +418,17 @@ class SkeletalRichEnumLite using ValuesFriend = SkeletalRichEnumValues; public: - static constexpr std::optional> value_of( - const std::string_view& name) + static constexpr std::optional value_of(const std::string_view& name) { return rich_enums_detail::value_of(name); } - static constexpr std::optional> value_of( - const BackingEnum& backing_enum) + static constexpr std::optional value_of(const BackingEnum& backing_enum) { return rich_enums_detail::value_of(backing_enum); } - static constexpr std::optional> value_of( + static constexpr std::optional value_of( std::underlying_type_t enum_integer) { return rich_enums_detail::value_of(enum_integer); diff --git a/test/enum_utils_test.cpp b/test/enum_utils_test.cpp index 0716928c..460b0f25 100644 --- a/test/enum_utils_test.cpp +++ b/test/enum_utils_test.cpp @@ -296,8 +296,7 @@ TEST(RichEnum, ValueOfName) } { - constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of("C_ONE").value(); - + constexpr TestRichEnum1 MY_VALUE = TestRichEnum1::value_of("C_ONE").value(); static_assert(MY_VALUE == TestRichEnum1::C_ONE()); } } @@ -315,8 +314,7 @@ TEST(RichEnum, ValueOfBackingEnum) { using BE = detail::TestRichEnum1BackingEnum; - constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of(BE::C_ONE).value(); - + constexpr TestRichEnum1 MY_VALUE = TestRichEnum1::value_of(BE::C_ONE).value(); static_assert(MY_VALUE == TestRichEnum1::C_ONE()); } } @@ -332,12 +330,22 @@ TEST(RichEnum, ValueOfUnderlyingInt) } { - constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of(19).value(); - + constexpr TestRichEnum1 MY_VALUE = TestRichEnum1::value_of(19).value(); static_assert(MY_VALUE == TestRichEnum1::C_ONE()); } } +TEST(RichEnum, ValueOfParityWithBuiltinEnums) +{ + // Optional values + { + static_assert(std::same_as, + decltype(magic_enum::enum_cast("ONE"))>); + static_assert( + std::same_as, decltype(TestRichEnum1::value_of("C_ONE"))>); + } +} + TEST(RichEnum, UniqueValuesArrays) { constexpr const auto& ENUM1_VALUES = TestRichEnum1::values();