From d33424c3771c702471840d1ab0fdcb27e6186a58 Mon Sep 17 00:00:00 2001 From: Neargye Date: Fri, 13 Dec 2019 20:31:55 +0500 Subject: [PATCH] improve cstring ctr --- include/nameof.hpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index abdad34..f40cf98 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -43,6 +43,12 @@ #include #include +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable : 26495) // Variable 'nameof::cstring::chars_' is uninitialized. +# pragma warning(disable : 26451) // Arithmetic overflow: 'strings_[static_cast(value) - min_v]' and 'indexes_[static_cast(value) - min_v]' using operator '-' on a 4 byte value and then casting the result to a 8 byte value. +#endif + // Checks nameof_type compiler compatibility. #if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) # undef NAMEOF_TYPE_SUPPORTED @@ -94,14 +100,8 @@ class [[nodiscard]] cstring { std::array chars_; - constexpr auto to_chars(std::string_view str) noexcept { - assert(str.size() == N); - decltype(chars_) chars = {}; - for (std::size_t i = 0; i < str.size() && i < N; ++i) { - chars[i] = str[i]; - } - return chars; - } + template + constexpr cstring(std::string_view str, std::index_sequence) noexcept : chars_{{str[I]..., '\0'}} {} public: using value_type = char; @@ -118,7 +118,9 @@ class [[nodiscard]] cstring { using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - constexpr explicit cstring(std::string_view str) noexcept : chars_{to_chars(str)} {} + constexpr explicit cstring(std::string_view str) noexcept : cstring{str, std::make_index_sequence{}} { + assert(str.size() == N); + } constexpr cstring() = delete; @@ -662,4 +664,8 @@ template // Obtains string name full type of expression, with reference and cv-qualifiers. #define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type() +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + #endif // NEARGYE_NAMEOF_HPP