From 9ab59decf69aaea379cc17ec92729d093ddc608c Mon Sep 17 00:00:00 2001 From: yudaichen <250074249@qq.com> Date: Wed, 15 Oct 2025 12:55:48 +0800 Subject: [PATCH] Fix MSVC C++20/23 module compatibility for static_str conversion operator - Add explicit str() method to static_str for explicit string_view conversion - Conditionally disable implicit operator string_view() in MSVC module mode - This resolves C2833 error when using MAGIC_ENUM_USE_STD_MODULE with MSVC - Non-module builds and other compilers remain unaffected --- include/magic_enum/magic_enum.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/magic_enum/magic_enum.hpp b/include/magic_enum/magic_enum.hpp index 00f8257..d99bb34 100644 --- a/include/magic_enum/magic_enum.hpp +++ b/include/magic_enum/magic_enum.hpp @@ -318,7 +318,14 @@ class static_str { constexpr std::uint16_t size() const noexcept { return N; } - constexpr operator string_view() const noexcept { return {data(), size()}; } + // MSVC C++20/23 module compatibility: explicit conversion method + [[nodiscard]] constexpr string_view str() const noexcept { + return string_view(data(), size()); + } + + #if !defined(MAGIC_ENUM_USE_STD_MODULE) || !defined(_MSC_VER) + constexpr operator string_view() const noexcept { return str(); } + #endif private: template @@ -886,7 +893,8 @@ inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v constexpr auto names(std::index_sequence) noexcept { - constexpr auto names = std::array{{enum_name_v[I]>...}}; + // MSVC module compatibility: explicit call to str() instead of implicit conversion + constexpr auto names = std::array{{enum_name_v[I]>.str()...}}; return names; }