| 1174 | // No bounds checking is performed: the behavior is undefined if index >= number of enum values. |
| 1175 | template <typename E, detail::enum_subtype S = detail::subtype_v<E>> |
| 1176 | [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_t<E, std::decay_t<E>> { |
| 1177 | using D = std::decay_t<E>; |
| 1178 | static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min."); |
| 1179 | |
| 1180 | if constexpr (detail::is_sparse_v<D, S>) { |
| 1181 | return MAGIC_ENUM_ASSERT(index < detail::count_v<D, S>), detail::values_v<D, S>[index]; |
| 1182 | } else { |
| 1183 | constexpr auto min = (S == detail::enum_subtype::flags) ? detail::log2(detail::min_v<D, S>) : detail::min_v<D, S>; |
| 1184 | |
| 1185 | return MAGIC_ENUM_ASSERT(index < detail::count_v<D, S>), detail::value<D, min, S>(index); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | // Returns enum value at specified index. |
| 1190 | template <typename E, std::size_t I, detail::enum_subtype S = detail::subtype_v<E>> |