From 8aeb6774132a01765d8c8679d016b728acd069f5 Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Fri, 23 Jun 2023 14:10:32 +0200 Subject: [PATCH] Replace union type holder to template extern variable (#62) --- include/nameof.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 2991602..4f7976a 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -968,7 +968,8 @@ constexpr auto n() noexcept { #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}); + constexpr auto name = pretty_name({__FUNCSIG__, + sizeof(__FUNCSIG__) - 18 + std::is_member_function_pointer_v}); #else constexpr auto name = string_view{}; #endif @@ -986,26 +987,25 @@ template From get_base_type(Type From::*); template -union union_type { - constexpr ~union_type() {} - char c = {}; - T f; +extern T nonexist_object; + +template +struct Store { + T v; }; -template -struct union_type_holder { - constexpr static union_type value; -}; +template +Store(T) -> Store; template -constexpr auto get_member_name() noexcept { +consteval auto get_member_name() noexcept { if constexpr (std::is_member_function_pointer_v) { return n(); } else { constexpr bool is_defined = sizeof(decltype(get_base_type(V))) != 0; static_assert(is_defined, "nameof::nameof_member member name can use only if the struct is already fully defined. Please use NAMEOF macro, or separate definition and declaration."); if constexpr (is_defined) { - return n::value.f.*V)>(); + return n.*V)}>(); } else { return ""; }