如何提振::函数和boost ::绑定工作 [英] how boost::function and boost::bind work

查看:78
本文介绍了如何提振::函数和boost ::绑定工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢有魔力盒散落在我的code ......究竟如何做到这两个类的工作,让基本的功能映射到一个函数对象,即使功能和LT;>有一个完全不同的参数设置到一个IM传递给的boost ::绑定

I dislike having magic boxes scattered all over my code...how exactly do these two classes work to allow basically any function to be mapped to a function object even if the function<> has a completely different parameter set to the one im passing to boost::bind

它甚至有不同的调用约定(即成员方法 __ thiscall VC下工作,但正常的功能一般是 __ CDECL __ STDCALL 对于那些需要用C兼容。

It even works with different calling conventions (i.e. member methods are __thiscall under VC, but "normal" functions are generally __cdecl or __stdcall for those that need to be compatible with C.

推荐答案

的boost ::功能允许用运营商任何东西()与正确的签名被绑定的参数,和你绑定的结果可以调用参数 INT ,因此它可以被绑定到功能和LT;无效(INT)方式&gt;

boost::function allows anything with an operator() with the right signature to be bound as the parameter, and the result of your bind can be called with a parameter int, so it can be bound to function<void(int)>.

这是它的工作原理(这说明适用都为的std ::功能

This is how it works (this description applies alike for std::function):

的boost ::绑定(安培;克拉斯::成员,例如,0,_1)返回这样一个对象

struct unspecified_type
{
  ... some members ...
  return_type operator()(int i) const { return instance->*&klass::member(0, i);
}

其中 return_type INT 克拉斯的签名被推断::成员,以及函数指针和绑定参数实际上存储在对象,但是这并不重要。

where the return_type and int are inferred from the signature of klass::member, and the function pointer and bound parameter are in fact stored in the object, but that's not important

现在,的boost ::功能不做任何类型检查:它会采取任何对象,任何签名,你在它的模板参数提供,并创建一个对象,它是可调用根据您的签名,并调用该对象。如果这是不可能的,这是一个编译错误。

Now, boost::function doesn't do any type checking: It will take any object and any signature you provide in its template parameter, and create an object that's callable according to your signature and calls the object. If that's impossible, it's a compile error.

的boost ::功能实际上是一个对象是这样的:

boost::function is actually an object like this:

template <class Sig>
class function
{
  function_impl<Sig>* f;
public:
  return_type operator()(argument_type arg0) const { return (*f)(arg0); }
};

其中 return_type argument_type 从提取西格˚F动态分配在堆上。这需要允许完全不相关的对象不同尺寸绑定到的boost ::功能

where the return_type and argument_type are extracted from Sig, and f is dynamically allocated on the heap. That's needed to allow completely unrelated objects with different sizes bind to boost::function.

function_impl 仅仅是一个抽象类

template <class Sig>
class function_impl
{
public:
  virtual return_type operator()(argument_type arg0) const=0;
};

这所有的工作类,从的boost ::功能派生的具体类。有一个为每个分配到的boost ::功能

The class that does all the work, is a concrete class derived from boost::function. There is one for each type of object you assign to 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功能:

That means in your case, the assignment to boost function:


  1. 实例化一个类型 function_impl_concrete&LT;无效(INT),unspecified_type&GT; (这是编译的时候,当然)

  2. 在堆上创建该类型的新对象

  3. 将此对象分配给升压功能::在F成员

  1. instantiates a type function_impl_concrete<void(int), unspecified_type> (that's compile time, of course)
  2. creates a new object of that type on the heap
  3. assigns this object to the f member of boost::function

当你调用这个函数对象,它会调用其实施对象的虚函数,这将直接调用原来的功能。

When you call the function object, it calls the virtual function of its implementation object, which will direct the call to your original function.

声明:注意,在本说明的名字是故意制成的。如有雷同,真正的人或字符...你知道的。的目的是为了说明原理

DISCLAIMER: Note that the names in this explanation are deliberately made up. Any resemblance to real persons or characters ... you know it. The purpose was to illustrate the principles.

这篇关于如何提振::函数和boost ::绑定工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆