From 2cb9751cb6e6e2d44883f95dafd6b12c911adb6a Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 21 Jul 2022 21:07:19 +0400 Subject: [PATCH] add nameof_member for mscv and c++20 --- include/nameof.hpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index cb47d8d..df868a3 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -96,7 +96,7 @@ #endif // Checks nameof_member compiler compatibility. -#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L # undef NAMEOF_MEMBER_SUPPORTED # define NAMEOF_MEMBER_SUPPORTED 1 #endif @@ -921,6 +921,8 @@ constexpr auto n() noexcept { if constexpr (custom_name.empty() && nameof_member_supported::value) { #if defined(__clang__) || defined(__GNUC__) constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); +#elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L + constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); #else constexpr auto name = string_view{}; #endif @@ -930,8 +932,38 @@ constexpr auto n() noexcept { } } +#if defined(__clang__) || defined(__GNUC__) template inline constexpr auto member_name_v = n(); +#elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L +template +From get_base_type(Type From::*); + +template +union union_type { + char c = {}; + T f; +}; + +template +inline constexpr auto v = T{}; + +template +constexpr auto get_member_name() noexcept { + if constexpr (std::is_member_function_pointer_v) { + return n(); + } else { + return n<&(constexpr_static_init>.f.*V)>(); + } +} + +template +inline constexpr auto member_name_v = get_member_name(); + +#else +template +inline constexpr auto member_name_v = cstring<0>{string_view{}}; +#endif } // namespace nameof::detail