如何获得 std::bind 函数签名?

问题描述

我正在尝试为每个类似函数的类型(例如函数、lambda、比较器)获取一个返回类型和一些参数。

/**
 * @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 ...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())> {};

这适用于除 std::bind 之外的所有内容,因为它有多个 operator() 函数。如何改进我的代码以从绑定中获取信息?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)