I'm trying to get a return type and a number of arguments for every function-like type (e.g functions, lambdas, comparators).
/**
* @brief Get some info about any function-like object at compile-time.
*
* @tparam F function template.
*/
template<typename F, typename = void>
struct function_traits;
// Catch functions
template<typename R, typename ...Args>
struct function_traits<R (Args...)>
{
using return_type = R;
static constexpr std::size_t argc = sizeof...(Args);
template<size_t i>
struct get_arg
{
using type = std::tuple_element_t<i, std::tuple<Args...>>;
};
template<size_t i>
using get_arg_t = typename get_arg<i>::type;
};
// Catch function pointers
template<typename R, typename ...Args>
struct function_traits<R (*)(Args...)> : function_traits<R (Args...)> {};
// Catch member functions
template<typename R, typename C, typename ...Args>
struct function_traits<R (C::*)(Args...)> : function_traits<R (Args...)> {};
// Catch member const functions
template<typename R, typename C, typename ...Args>
struct function_traits<R (C::*)(Args...) const> : function_traits<R (C::*)(Args...)> {};
// Catch lambda and structs with one operator()
template<typename F>
struct function_traits<F, void> : function_traits<decltype(&F::operator())> {};
This works for everything but std::bind
because it has multiple operator()
functions. How can I improve my code to get info from binds?