<i id='NT6wG'><tr id='NT6wG'><dt id='NT6wG'><q id='NT6wG'><span id='NT6wG'><b id='NT6wG'><form id='NT6wG'><ins id='NT6wG'></ins><ul id='NT6wG'></ul><sub id='NT6wG'></sub></form><legend id='NT6wG'></legend><bdo id='NT6wG'><pre id='NT6wG'><center id='NT6wG'></center></pre></bdo></b><th id='NT6wG'></th></span></q></dt></tr></i><div id='NT6wG'><tfoot id='NT6wG'></tfoot><dl id='NT6wG'><fieldset id='NT6wG'></fieldset></dl></div>

    <legend id='NT6wG'><style id='NT6wG'><dir id='NT6wG'><q id='NT6wG'></q></dir></style></legend>

    <small id='NT6wG'></small><noframes id='NT6wG'>

    1. <tfoot id='NT6wG'></tfoot>
        <bdo id='NT6wG'></bdo><ul id='NT6wG'></ul>

    2. boost::function 和 boost::bind 如何工作

      时间:2023-06-29

          <bdo id='c207N'></bdo><ul id='c207N'></ul>

              <tbody id='c207N'></tbody>

              <small id='c207N'></small><noframes id='c207N'>

            • <tfoot id='c207N'></tfoot>
            • <legend id='c207N'><style id='c207N'><dir id='c207N'><q id='c207N'></q></dir></style></legend>
              <i id='c207N'><tr id='c207N'><dt id='c207N'><q id='c207N'><span id='c207N'><b id='c207N'><form id='c207N'><ins id='c207N'></ins><ul id='c207N'></ul><sub id='c207N'></sub></form><legend id='c207N'></legend><bdo id='c207N'><pre id='c207N'><center id='c207N'></center></pre></bdo></b><th id='c207N'></th></span></q></dt></tr></i><div id='c207N'><tfoot id='c207N'></tfoot><dl id='c207N'><fieldset id='c207N'></fieldset></dl></div>

                本文介绍了boost::function 和 boost::bind 如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我不喜欢在我的代码中散布魔术盒...这两个类究竟如何工作以允许基本上任何函数映射到函数对象,即使函数<>具有完全不同的参数设置为一个我传递给 boost::bind

                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

                它甚至适用于不同的调用约定(即成员方法在 VC 下是 __thiscall,但正常"函数通常是 __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::function 允许任何带有 operator() 和正确签名的东西被绑定为参数,你的绑定结果可以用参数int调用,所以它可以绑定到function.

                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::function):

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

                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_typeint是从klass::member的签名推断出来的,实际上是函数指针和绑定参数存储在对象中,但这并不重要

                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::function 不做任何类型检查:它将接受您在模板参数中提供的任何对象和任何签名,并创建一个可根据您的签名和调用对象.如果这是不可能的,那就是编译错误.

                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::function 实际上是一个这样的对象:

                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_typeargument_type 是从Sig 中提取出来的,f 在堆上动态分配.这需要允许具有不同大小的完全不相关的对象绑定到 boost::function.

                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::function 派生的具体类.您分配给 boost::function

                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(当然是编译时间)
                2. 在堆上创建一个该类型的新对象
                3. 将此对象分配给 boost::function 的 f 成员

                当您调用函数对象时,它会调用其实现对象的虚函数,从而将调用定向到您的原始函数.

                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::function 和 boost::bind 如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在容器中存储具有不同签名的功能对象? 下一篇:C++11 等价于 boost shared_mutex

                相关文章

                  <bdo id='l9Pvb'></bdo><ul id='l9Pvb'></ul>

                  <legend id='l9Pvb'><style id='l9Pvb'><dir id='l9Pvb'><q id='l9Pvb'></q></dir></style></legend>
                1. <tfoot id='l9Pvb'></tfoot>

                  <small id='l9Pvb'></small><noframes id='l9Pvb'>

                2. <i id='l9Pvb'><tr id='l9Pvb'><dt id='l9Pvb'><q id='l9Pvb'><span id='l9Pvb'><b id='l9Pvb'><form id='l9Pvb'><ins id='l9Pvb'></ins><ul id='l9Pvb'></ul><sub id='l9Pvb'></sub></form><legend id='l9Pvb'></legend><bdo id='l9Pvb'><pre id='l9Pvb'><center id='l9Pvb'></center></pre></bdo></b><th id='l9Pvb'></th></span></q></dt></tr></i><div id='l9Pvb'><tfoot id='l9Pvb'></tfoot><dl id='l9Pvb'><fieldset id='l9Pvb'></fieldset></dl></div>