diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp index 1fb28732c..0cf242754 100644 --- a/src/entt/core/type_traits.hpp +++ b/src/entt/core/type_traits.hpp @@ -698,6 +698,38 @@ public: template using member_class_t = typename member_class::type; +/** + * @brief Extracts the n-th argument of a given function or member function. + * @tparam Index The index of the argument to extract. + * @tparam Candidate A valid function, member function or data member. + */ +template +class nth_argument { + template + static constexpr type_list pick_up(Ret (*)(Args...)); + + template + static constexpr type_list pick_up(Ret (Class ::*)(Args...)); + + template + static constexpr type_list pick_up(Ret (Class ::*)(Args...) const); + + template + static constexpr type_list pick_up(Type Class ::*); + +public: + /*! @brief N-th argument of the given function or member function. */ + using type = type_list_element_t; +}; + +/** + * @brief Helper type. + * @tparam Index The index of the argument to extract. + * @tparam Candidate A valid function, member function or data member. + */ +template +using nth_argument_t = typename nth_argument::type; + } // namespace entt #endif diff --git a/test/entt/core/type_traits.cpp b/test/entt/core/type_traits.cpp index 5726b8dfe..1d035c18e 100644 --- a/test/entt/core/type_traits.cpp +++ b/test/entt/core/type_traits.cpp @@ -20,6 +20,20 @@ struct nlohmann_json_like final { } }; +struct clazz { + char foo(int) { + return {}; + } + + int bar(double, float) const { + return {}; + } + + bool quux; +}; + +void free_function(int, const double &) {} + TEST(SizeOf, Functionalities) { static_assert(entt::size_of_v == 0u); static_assert(entt::size_of_v == sizeof(char)); @@ -177,23 +191,19 @@ TEST(ConstnessAs, Functionalities) { } TEST(MemberClass, Functionalities) { - struct clazz { - char foo(int) { - return {}; - } - - int bar(double, float) const { - return {}; - } - - bool quux; - }; - static_assert(std::is_same_v>); static_assert(std::is_same_v>); static_assert(std::is_same_v>); } +TEST(NthArgument, Functionalities) { + static_assert(std::is_same_v, int>); + static_assert(std::is_same_v, const double &>); + static_assert(std::is_same_v, double>); + static_assert(std::is_same_v, float>); + static_assert(std::is_same_v, bool>); +} + TEST(Tag, Functionalities) { using namespace entt::literals; static_assert(entt::tag<"foobar"_hs>::value == entt::hashed_string::value("foobar"));