diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp index 7c4b09f41..9561c7b5a 100644 --- a/src/entt/core/type_traits.hpp +++ b/src/entt/core/type_traits.hpp @@ -3,6 +3,7 @@ #include +#include #include #include "../config/config.h" #include "../core/hashed_string.hpp" @@ -169,6 +170,37 @@ template constexpr auto is_equality_comparable_v = is_equality_comparable::value; +/** + * @brief Extracts the class of a non-static member object or function. + * @tparam Member A pointer to a non-static member object or function. + */ +template +class member_class { + static_assert(std::is_member_pointer_v); + + template + static Class * clazz(Ret(Class:: *)(Args...)); + + template + static Class * clazz(Ret(Class:: *)(Args...) const); + + template + static Class * clazz(Type Class:: *); + +public: + /*! @brief The class of the given non-static member object or function. */ + using type = std::remove_pointer_t()))>; +}; + + +/** + * @brief Helper type. + * @tparam Member A pointer to a non-static member object or function. + */ +template +using member_class_t = typename member_class::type; + + } diff --git a/test/entt/core/type_traits.cpp b/test/entt/core/type_traits.cpp index c1f9f9e00..42252611d 100644 --- a/test/entt/core/type_traits.cpp +++ b/test/entt/core/type_traits.cpp @@ -22,3 +22,15 @@ TEST(IsEqualityComparable, Functionalities) { ASSERT_TRUE(entt::is_equality_comparable_v); ASSERT_FALSE(entt::is_equality_comparable_v); } + +TEST(MemberClass, Functionalities) { + struct clazz { + char foo(int) { return {}; } + int bar(double, float) const { return {}; } + bool quux; + }; + + ASSERT_TRUE((std::is_same_v>)); + ASSERT_TRUE((std::is_same_v>)); + ASSERT_TRUE((std::is_same_v>)); +}