如果不花很长时间来审查boost源代码,有人可以帮助我快速了解一下boost bind的实现方式吗?
发布于 2018-07-27 08:50:48
我喜欢这段bind
来源:
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()
定义。例如,最简单的:
result_type operator()()
{
list0 a;
BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}
我们可以看到BOOST_BIND_RETURN
宏扩展到return
此时线条更像return l_(type...)
一个参数版本:
template<class A1> result_type operator()(A1 & a1)
{
list1<A1 &> a(a1);
BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}
这些listN
类是参数列表的包装器。他们超负荷调用了unwrap
功能。忽略一些编译器特定的重载:
// 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
是函数参数的类型bind
。R
是返回类型。L
往往是参数类型列表。
发布于 2018-07-27 09:26:45
我认为它是一个模板类,它为你想要绑定的参数声明一个成员变量,并为其余的参数声明overloads()。
https://stackoverflow.com/questions/-100000432
复制相似问题