首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >一般来说,boost绑定如何在幕后工作?

一般来说,boost绑定如何在幕后工作?
EN

Stack Overflow用户
提问于 2018-07-27 00:02:36
回答 2查看 0关注 0票数 0

如果不花很长时间来审查boost源代码,有人可以帮助我快速了解一下boost bind的实现方式吗?

EN

回答 2

Stack Overflow用户

发布于 2018-07-27 08:50:48

我喜欢这段bind来源:

代码语言:javascript
复制
template<class R, class F, class L> class bind_t
{
public:

    typedef bind_t this_type;

    bind_t(F f, L const & l): f_(f), l_(l) {}

#define BOOST_BIND_RETURN return
#include <boost/bind/bind_template.hpp>
#undef BOOST_BIND_RETURN

};

bind_template头扩展到内联的列表operator()定义。例如,最简单的:

代码语言:javascript
复制
result_type operator()()
{
    list0 a;
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

我们可以看到BOOST_BIND_RETURN宏扩展到return此时线条更像return l_(type...)

一个参数版本:

代码语言:javascript
复制
template<class A1> result_type operator()(A1 & a1)
{
    list1<A1 &> a(a1);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

这些listN类是参数列表的包装器。他们超负荷调用了unwrap功能。忽略一些编译器特定的重载:

代码语言:javascript
复制
// unwrap

template<class F> inline F & unwrap(F * f, long)
{
    return *f;
}

template<class F> inline F & unwrap(reference_wrapper<F> * f, int)
{
    return f->get();
}

template<class F> inline F & unwrap(reference_wrapper<F> const * f, int)
{
    return f->get();
}

命名约定似乎是:F是函数参数的类型bindR是返回类型。L往往是参数类型列表。

票数 0
EN

Stack Overflow用户

发布于 2018-07-27 09:26:45

我认为它是一个模板类,它为你想要绑定的参数声明一个成员变量,并为其余的参数声明overloads()。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100000432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档