1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00

workaround std::array in some cases is missing constexpr on some of its functions (#76)

This commit is contained in:
Daniil Goncharov 2021-03-10 11:05:44 +02:00 committed by GitHub
parent c1e7c1475a
commit ed2dbfd46c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -169,7 +169,7 @@ class static_string {
assert(str.size() == N);
}
constexpr const char* data() const noexcept { return chars_.data(); }
constexpr const char* data() const noexcept { return chars_; }
constexpr std::size_t size() const noexcept { return N; }
@ -177,9 +177,9 @@ class static_string {
private:
template <std::size_t... I>
constexpr static_string(string_view str, std::index_sequence<I...>) noexcept : chars_{{str[I]..., '\0'}} {}
constexpr static_string(string_view str, std::index_sequence<I...>) noexcept : chars_{str[I]..., '\0'} {}
const std::array<char, N + 1> chars_;
char chars_[N + 1];
};
template <>
@ -235,6 +235,11 @@ constexpr std::size_t find(string_view str, char c) noexcept {
}
}
template <typename T, std::size_t N, std::size_t... I>
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N], std::index_sequence<I...>) {
return {{a[I]...}};
}
template <typename BinaryPredicate>
constexpr bool cmp_equal(string_view lhs, string_view rhs, BinaryPredicate&& p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) {
#if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
@ -409,9 +414,9 @@ constexpr E value(std::size_t i) noexcept {
}
template <std::size_t N>
constexpr std::size_t values_count(const std::array<bool, N>& valid) noexcept {
constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
auto count = std::size_t{0};
for (std::size_t i = 0; i < valid.size(); ++i) {
for (std::size_t i = 0; i < N; ++i) {
if (valid[i]) {
++count;
}
@ -423,17 +428,17 @@ constexpr std::size_t values_count(const std::array<bool, N>& valid) noexcept {
template <typename E, bool IsFlags, int Min, std::size_t... I>
constexpr auto values(std::index_sequence<I...>) noexcept {
static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
constexpr std::array<bool, sizeof...(I)> valid{{is_valid<E, value<E, Min, IsFlags>(I)>()...}};
constexpr bool valid[sizeof...(I)] = {is_valid<E, value<E, Min, IsFlags>(I)>()...};
constexpr std::size_t count = values_count(valid);
std::array<E, count> values{};
E values[count] = {};
for (std::size_t i = 0, v = 0; v < count; ++i) {
if (valid[i]) {
values[v++] = value<E, Min, IsFlags>(i);
}
}
return values;
return to_array(values, std::make_index_sequence<count>{});
}
template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>