This commit is contained in:
kamchatka-volcano 2022-12-07 00:24:06 +03:00 committed by GitHub
parent f7b2f34053
commit 70ca094db8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -979,14 +979,16 @@ union union_type {
}; };
template <typename T> template <typename T>
inline constexpr auto static_v = T{}; struct union_type_holder {
constexpr static union_type<T> value;
};
template <auto V> template <auto V>
constexpr auto get_member_name() noexcept { constexpr auto get_member_name() noexcept {
if constexpr (std::is_member_function_pointer_v<decltype(V)>) { if constexpr (std::is_member_function_pointer_v<decltype(V)>) {
return n<V>(); return n<V>();
} else { } else {
return n<V, &(static_v<union_type<decltype(get_base_type(V))>>.f.*V)>(); return n<V, &(union_type_holder<decltype(get_base_type(V))>::value.f.*V)>();
} }
} }

View file

@ -118,6 +118,19 @@ struct TestRtti{
struct Derived : Base {}; 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; SomeStruct struct_var;
Long othervar; Long othervar;
SomeStruct* ptr_s = &struct_var; SomeStruct* ptr_s = &struct_var;
@ -916,6 +929,8 @@ TEST_CASE("NAMEOF_MEMBER") {
REQUIRE(NAMEOF_MEMBER(&Long::LL::field) == "field"); REQUIRE(NAMEOF_MEMBER(&Long::LL::field) == "field");
constexpr auto member_ptr = &SomeStruct::somefield; constexpr auto member_ptr = &SomeStruct::somefield;
REQUIRE(NAMEOF_MEMBER(member_ptr) == "somefield"); REQUIRE(NAMEOF_MEMBER(member_ptr) == "somefield");
REQUIRE(NAMEOF_MEMBER(&StructMemberInitializationUsingNameof::teststringfield) == "teststringfield");
REQUIRE(NAMEOF_MEMBER(&StructWithNonConstexprDestructor::somefield) == "somefield");
} }
TEST_CASE("nameof_member") { TEST_CASE("nameof_member") {
@ -924,6 +939,8 @@ TEST_CASE("nameof_member") {
REQUIRE(nameof::nameof_member<&Long::LL::field>() == "field"); REQUIRE(nameof::nameof_member<&Long::LL::field>() == "field");
constexpr auto member_ptr = &SomeStruct::somefield; constexpr auto member_ptr = &SomeStruct::somefield;
REQUIRE(nameof::nameof_member<member_ptr>() == "somefield"); REQUIRE(nameof::nameof_member<member_ptr>() == "somefield");
REQUIRE(nameof::nameof_member<&StructMemberInitializationUsingNameof::teststringfield>() == "teststringfield");
REQUIRE(nameof::nameof_member<&StructWithNonConstexprDestructor::somefield>() == "somefield");
} }
#endif #endif