mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
Explicitly mark the result of detail::names as constexpr (#305)
It might make no sense, but on MSVC it can generate a compile-time error, especially if an enumerator's value is out of range. Example: error C3615: constexpr function 'magic_enum::detail::names' cannot result in a constant expression ... note: failure was caused by call of undefined function or one not declared 'constexpr' ... note: see usage of '__builtin_array_init_helper'
This commit is contained in:
parent
5cf4eb3a53
commit
5523803cfc
2 changed files with 14 additions and 1 deletions
|
|
@ -829,7 +829,8 @@ inline constexpr auto max_v = (count_v<E, S> > 0) ? static_cast<U>(values_v<E, S
|
|||
|
||||
template <typename E, enum_subtype S, std::size_t... I>
|
||||
constexpr auto names(std::index_sequence<I...>) noexcept {
|
||||
return std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E, S>[I]>...}};
|
||||
constexpr auto result = std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E, S>[I]>...}};
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename E, enum_subtype S>
|
||||
|
|
|
|||
|
|
@ -510,6 +510,11 @@ class a_foo2 {
|
|||
};
|
||||
} // namespace
|
||||
|
||||
enum class LargeNumbers {
|
||||
First = -1024,
|
||||
Second = 1024
|
||||
};
|
||||
|
||||
TEST_CASE("enum_name") {
|
||||
SECTION("automatic storage") {
|
||||
constexpr Color cr = Color::RED;
|
||||
|
|
@ -646,6 +651,13 @@ TEST_CASE("enum_name") {
|
|||
REQUIRE(enum_name<Binary::ONE>() == "ONE");
|
||||
REQUIRE(enum_name<MaxUsedAsInvalid::ONE>() == "ONE");
|
||||
}
|
||||
|
||||
SECTION("empty if the value is out of range") {
|
||||
const auto ln_value = GENERATE(LargeNumbers::First, LargeNumbers::Second);
|
||||
const auto ln_name = enum_name(ln_value);
|
||||
|
||||
REQUIRE(ln_name.empty());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("enum_names") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue