From c4d533d9d2049a2ea8c038c73aa806a702586675 Mon Sep 17 00:00:00 2001 From: Ryan Saunders Date: Tue, 24 Jun 2025 10:08:45 -0700 Subject: [PATCH] Move calling convention into a macro that can be defined per-compiler --- include/magic_enum/magic_enum.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/magic_enum/magic_enum.hpp b/include/magic_enum/magic_enum.hpp index b093772..907b670 100644 --- a/include/magic_enum/magic_enum.hpp +++ b/include/magic_enum/magic_enum.hpp @@ -98,6 +98,14 @@ # define MAGIC_ENUM_SUPPORTED_ALIASES 1 #endif +// Specify the calling convention for compilers that need it in order to get reliable mangled names under different +// compiler flags. In particular, MSVC allows changing the default calling convention on x86. +#if defined(__clang__) || defined(__GNUC__) +#define MAGIC_ENUM_CALLING_CONVENTION +#elif defined(_MSC_VER) +#define MAGIC_ENUM_CALLING_CONVENTION __cdecl +#endif + // Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128. // If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN. #if !defined(MAGIC_ENUM_RANGE_MIN) @@ -418,7 +426,7 @@ template inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; template -constexpr auto __cdecl n() noexcept { +constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); if constexpr (supported::value) { @@ -494,7 +502,7 @@ template inline constexpr auto type_name_v = type_name(); template -constexpr auto __cdecl n() noexcept { +constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); if constexpr (supported::value) { @@ -566,7 +574,7 @@ constexpr auto __cdecl n() noexcept { #if defined(MAGIC_ENUM_VS_2017_WORKAROUND) template -constexpr auto __cdecl n() noexcept { +constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); # if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN)