微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何获得 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 举报,一经查实,本站将立刻删除。