boost::function 和 boost::bind 的工作原理
2011-03-24 15:21
288 查看
boost::bind(&klass::member, instance, 0, _1) 返回一个函数对象,类似于:
struct unspecified_type {
... some members ...
return_type operator()(int i) const { return instance->*&klass::member(0, i);
}
返回值 return_type 和参数 int 是通过 klass::member 的函数签名以及 bind 绑定式的参数列表推断出来的。
boost::function 并不进行类型检查,可以使用任意函数签名式(模版参数)和任意可调用物来创建一个函数对象,并按照指定的函数签名式执行对象调用,如果该调用不符合函数签名式则会产生编译错误。
boost::function 实际是一个函数对象,类似于:
template <class Sig>
class function {
function_impl<Sig>* f;
public:
return_type operator()(argument_type arg0) const { return (*f)(arg0); }
};
返回值类型 return_type 和参数类型 argument_type 是从模版参数 Sig 中提取出来的,成员 f 是在 heap 上动态分配的,使得任意大小的函数对象都可以绑定到 boost::function 上。
function_impl 只是个 abstract class
template <class Sig>
class function_impl {
public:
virtual return_type operator()(argument_type arg0) const=0;
};
负责具体工作的是派生自 boost::function_impl 的实现类,每种赋值给 boost::function 的函数类型都有一个具体的对象类型:
template <class Sig, class Object>
class function_impl_concrete : public function_impl<Sig> {
Object o
public:
virtual return_type operator()(argument_type arg0) const=0 { return o(arg0); }
};
这意味着在实际运用中将可调用物赋值给 boost::function 将发生下面的事情:
1. 实例化一个函数对象类型 function_impl_concrete<void(int), unsepcified_type> (当然,这是发生在编译期);
2. 在 heap 上动态创建一个该类型的对象;
3. 将该对象赋值给 boost::function 的成员f;
当调用该函数对象时,将执行实现类上的虚函数,进而执行原始的可调用物。
参考资料
1. how boost::function and boost::bind work
2. 侯捷:Boost.Function内核剖析
3. Code Walk-through: How exactly does bind work (with placeholders) ?
struct unspecified_type {
... some members ...
return_type operator()(int i) const { return instance->*&klass::member(0, i);
}
返回值 return_type 和参数 int 是通过 klass::member 的函数签名以及 bind 绑定式的参数列表推断出来的。
boost::function 并不进行类型检查,可以使用任意函数签名式(模版参数)和任意可调用物来创建一个函数对象,并按照指定的函数签名式执行对象调用,如果该调用不符合函数签名式则会产生编译错误。
boost::function 实际是一个函数对象,类似于:
template <class Sig>
class function {
function_impl<Sig>* f;
public:
return_type operator()(argument_type arg0) const { return (*f)(arg0); }
};
返回值类型 return_type 和参数类型 argument_type 是从模版参数 Sig 中提取出来的,成员 f 是在 heap 上动态分配的,使得任意大小的函数对象都可以绑定到 boost::function 上。
function_impl 只是个 abstract class
template <class Sig>
class function_impl {
public:
virtual return_type operator()(argument_type arg0) const=0;
};
负责具体工作的是派生自 boost::function_impl 的实现类,每种赋值给 boost::function 的函数类型都有一个具体的对象类型:
template <class Sig, class Object>
class function_impl_concrete : public function_impl<Sig> {
Object o
public:
virtual return_type operator()(argument_type arg0) const=0 { return o(arg0); }
};
这意味着在实际运用中将可调用物赋值给 boost::function 将发生下面的事情:
1. 实例化一个函数对象类型 function_impl_concrete<void(int), unsepcified_type> (当然,这是发生在编译期);
2. 在 heap 上动态创建一个该类型的对象;
3. 将该对象赋值给 boost::function 的成员f;
当调用该函数对象时,将执行实现类上的虚函数,进而执行原始的可调用物。
参考资料
1. how boost::function and boost::bind work
2. 侯捷:Boost.Function内核剖析
3. Code Walk-through: How exactly does bind work (with placeholders) ?
相关文章推荐
- muduo2 面向对象编程风格 VS 基于对象编程风格(boost::bind/function)
- boost::function 通过boost::bind调用类成员函数
- 以boost function和bind取代虚函数
- 以boost::function和boost:bind取代虚函数
- 以boost::function和boost:bind取代虚函数
- boost::function 和boost::bind
- 【Boost】以boost::function和boost:bind取代虚函数
- Boost.Bind和Boost.Function的一种搭配使用法
- boost::bind\boost::function
- 以boost::function和boost:bind取代虚函数
- 以boost::function和boost:bind取代虚函数
- boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
- 【Boost】boost库中function和bind一起使用的技巧(一)
- 以boost::function和boost:bind取代虚函数
- 函数指针&绑定: boost::functoin/std::function/bind
- 以boost::function和boost:bind取代虚函数
- boost::bind 与 boost::function 的使用方法例子
- 使用boost::bind和boost::function实现工厂模式
- 以boost::function和boost:bind取代虚函数
- 函数指针&绑定: boost::functoin/std::function/bind