From f57dcf410a8615c4ec59604cf05460315539bfe6 Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 10 Mar 2021 11:39:56 +0200 Subject: [PATCH] workaround std::array in some cases is missing constexpr on some of its functions --- include/nameof.hpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index b7f4616..b6e66b7 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -204,7 +204,7 @@ class [[nodiscard]] cstring { cstring& operator=(cstring&&) = default; - [[nodiscard]] constexpr const_pointer data() const noexcept { return chars_.data(); } + [[nodiscard]] constexpr const_pointer data() const noexcept { return chars_; } [[nodiscard]] constexpr size_type size() const noexcept { return N; } @@ -226,8 +226,6 @@ class [[nodiscard]] cstring { [[nodiscard]] constexpr const_reference operator[](size_type i) const noexcept { return assert(i < size()), chars_[i]; } - [[nodiscard]] constexpr const_reference at(size_type i) const { return assert(i < size()), chars_.at(i); } - [[nodiscard]] constexpr const_reference front() const noexcept { return chars_[0]; } [[nodiscard]] constexpr const_reference back() const noexcept { return chars_[N]; } @@ -252,9 +250,9 @@ class [[nodiscard]] cstring { private: template - constexpr cstring(string_view str, std::index_sequence) noexcept : chars_{{str[I]..., '\0'}} {} + constexpr cstring(string_view str, std::index_sequence) noexcept : chars_{str[I]..., '\0'} {} - std::array chars_; + char chars_[N + 1]; }; template @@ -407,6 +405,11 @@ constexpr string_view pretty_name(string_view name, bool remove_suffix = true) n return {}; // Invalid name. } +template +constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) { + return {{a[I]...}}; +} + template constexpr bool cmp_less(L lhs, R rhs) noexcept { static_assert(std::is_integral_v && std::is_integral_v, "nameof::detail::cmp_less requires integral type."); @@ -539,9 +542,9 @@ constexpr E value(std::size_t i) noexcept { } template -constexpr std::size_t values_count(const std::array& 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; } @@ -553,17 +556,17 @@ constexpr std::size_t values_count(const std::array& valid) noexcept { template constexpr auto values(std::index_sequence) noexcept { static_assert(is_enum_v, "nameof::detail::values requires enum type."); - constexpr std::array valid{{is_valid(I)>()...}}; + constexpr bool valid[sizeof...(I)] = {is_valid(I)>()...}; constexpr std::size_t count = values_count(valid); - std::array values{}; + E values[count] = {}; for (std::size_t i = 0, v = 0; v < count; ++i) { if (valid[i]) { values[v++] = value(i); } } - return values; + return to_array(values, std::make_index_sequence{}); } template >