Last active
September 7, 2020 14:05
-
-
Save SeanCline/8763331 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Usage: | |
* void test(string s, int n) { } | |
* cout << function_traits<decltype(test)>::arity << endl; //< Prints 2. | |
* function_traits<decltype(test)>::arg<0>::type s; //< Declares a string. | |
* function_traits<decltype(test)>::arg<1>::type n; //< Declares an int. | |
*/ | |
namespace utility { | |
template<typename FunctionType> | |
struct function_traits : function_traits<std::function<FunctionType>> {}; | |
template<typename Ret, typename ...Args> | |
struct function_traits<std::function<Ret(Args...)>> { //< Exploit std::function to unpack arguments. | |
using return_type = Ret; //< Return type of the function. | |
using args_tuple = std::tuple<Args...>; //< A tuple of the function's arguments. | |
static const size_t arity = sizeof...(Args); //< Number of arguments. | |
template <size_t N> | |
struct arg { | |
// Exploit tuple_element to get the type of an argument. | |
using type = typename std::tuple_element<N, std::tuple<Args...>>::type; //< Type of the nth argument. | |
}; | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment