MCPcopy Create free account
hub / github.com/Neargye/magic_enum / enum_cast

Function enum_cast

include/magic_enum/magic_enum.hpp:1374–1398  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1372// Returns optional with enum value.
1373template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
1374[[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, optional<std::decay_t<E>>> {
1375 using D = std::decay_t<E>;
1376 static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
1377
1378 if constexpr (detail::is_sparse_v<D, S> || (S == detail::enum_subtype::flags)) {
1379#if defined(MAGIC_ENUM_ENABLE_HASH)
1380 return detail::constexpr_switch<&detail::values_v<D, S>, detail::case_call_t::value>(
1381 [](D v) { return optional<D>{v}; },
1382 static_cast<D>(value),
1383 detail::default_result_type_lambda<optional<D>>);
1384#else
1385 for (std::size_t i = 0; i < detail::count_v<D, S>; ++i) {
1386 if (value == static_cast<underlying_type_t<D>>(enum_value<D, S>(i))) {
1387 return static_cast<D>(value);
1388 }
1389 }
1390 return {}; // Invalid value or out of range.
1391#endif
1392 } else {
1393 if (value >= detail::min_v<D, S> && value <= detail::max_v<D, S>) {
1394 return static_cast<D>(value);
1395 }
1396 return {}; // Invalid value or out of range.
1397 }
1398}
1399
1400// Returns enum value from name.
1401// Returns optional with enum value.

Callers

nothing calls this directly

Calls 1

cmp_equalFunction · 0.85

Tested by

no test coverage detected