From 70ca094db8f7179ea5c9dc6912e8900807a0b80a Mon Sep 17 00:00:00 2001 From: kamchatka-volcano Date: Wed, 7 Dec 2022 00:24:06 +0300 Subject: [PATCH] Fix #49 (#50) --- include/nameof.hpp | 6 ++++-- test/test.cpp | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 7382cf6..44230cc 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -979,14 +979,16 @@ union union_type { }; template -inline constexpr auto static_v = T{}; +struct union_type_holder { + constexpr static union_type value; +}; template constexpr auto get_member_name() noexcept { if constexpr (std::is_member_function_pointer_v) { return n(); } else { - return n>.f.*V)>(); + return n::value.f.*V)>(); } } diff --git a/test/test.cpp b/test/test.cpp index 22f7803..21223a8 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -118,6 +118,19 @@ struct TestRtti{ struct Derived : Base {}; }; +#if defined(NAMEOF_MEMBER_SUPPORTED) && NAMEOF_MEMBER_SUPPORTED + +struct StructMemberInitializationUsingNameof{ + std::string teststringfield = std::string{nameof::nameof_member<&StructMemberInitializationUsingNameof::teststringfield>()}; +}; + +struct StructWithNonConstexprDestructor{ + ~StructWithNonConstexprDestructor(){} + int somefield; +}; + +#endif + SomeStruct struct_var; Long othervar; SomeStruct* ptr_s = &struct_var; @@ -916,6 +929,8 @@ TEST_CASE("NAMEOF_MEMBER") { REQUIRE(NAMEOF_MEMBER(&Long::LL::field) == "field"); constexpr auto member_ptr = &SomeStruct::somefield; REQUIRE(NAMEOF_MEMBER(member_ptr) == "somefield"); + REQUIRE(NAMEOF_MEMBER(&StructMemberInitializationUsingNameof::teststringfield) == "teststringfield"); + REQUIRE(NAMEOF_MEMBER(&StructWithNonConstexprDestructor::somefield) == "somefield"); } TEST_CASE("nameof_member") { @@ -924,6 +939,8 @@ TEST_CASE("nameof_member") { REQUIRE(nameof::nameof_member<&Long::LL::field>() == "field"); constexpr auto member_ptr = &SomeStruct::somefield; REQUIRE(nameof::nameof_member() == "somefield"); + REQUIRE(nameof::nameof_member<&StructMemberInitializationUsingNameof::teststringfield>() == "teststringfield"); + REQUIRE(nameof::nameof_member<&StructWithNonConstexprDestructor::somefield>() == "somefield"); } #endif