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

      <tfoot id='N84JT'></tfoot>
    2. <small id='N84JT'></small><noframes id='N84JT'>

      1. 将 boost::function 降级为普通函数指针

        demote boost::function to a plain function pointer(将 boost::function 降级为普通函数指针)
        <i id='qsJj6'><tr id='qsJj6'><dt id='qsJj6'><q id='qsJj6'><span id='qsJj6'><b id='qsJj6'><form id='qsJj6'><ins id='qsJj6'></ins><ul id='qsJj6'></ul><sub id='qsJj6'></sub></form><legend id='qsJj6'></legend><bdo id='qsJj6'><pre id='qsJj6'><center id='qsJj6'></center></pre></bdo></b><th id='qsJj6'></th></span></q></dt></tr></i><div id='qsJj6'><tfoot id='qsJj6'></tfoot><dl id='qsJj6'><fieldset id='qsJj6'></fieldset></dl></div>
        <tfoot id='qsJj6'></tfoot>
            <tbody id='qsJj6'></tbody>
        • <legend id='qsJj6'><style id='qsJj6'><dir id='qsJj6'><q id='qsJj6'></q></dir></style></legend>

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

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

                  本文介绍了将 boost::function 降级为普通函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  想要将 boost::bind 传递给需要普通函数指针(相同签名)的方法.

                  want to pass boost::bind to a method expecting a plain function pointer (same signature).

                  typedef void TriggerProc_type(Variable*,void*);
                  void InitVariable(TriggerProc_type *proc);
                  boost::function<void (Variable*, void*)> triggerProc ...
                  InitVariable(triggerProc);
                  
                  error C2664: 'InitVariable' : cannot convert parameter 1 from 
                  'boost::function<Signature>' to 'void (__cdecl *)(type *,void *)'
                  

                  我可以避免存储 boost::function 并直接传递绑定的函子,但是我得到了类似的错误:

                  I can avoid storing a boost::function and just pass the bound functor directly, but then I get similar error:

                  error C2664: 'blah(void (__cdecl *)(type *,void *))' : cannot convert parameter
                  1 from 'boost::_bi::bind_t<R,F,L>' to 'void (__cdecl *)(type *,void *)'
                  

                  推荐答案

                  有没有人注意到接受的答案只有效与琐碎的案件?function<>::target() 将返回可以绑定到 C 回调的对象的唯一方法是,如果它是用可以绑定到 C 回调的对象构造的.如果是这种情况,那么您可以直接绑定它并跳过所有函数<>开始时的废话.

                  Has anyone noticed that the accepted answer only works with trivial cases? The only way that function<>::target() will return an object that can be bound to a C callback, is if it was constructed with an object that can be bound to a C callback. If that's the case, then you could have bound it directly and skipped all of the function<> nonsense to begin with.

                  如果你仔细想想,这个问题没有任何神奇的解决方案.C 样式的回调存储为指向可执行代码的单个指针.任何重要的 boost::function<> 都至少需要两个指针:一个指向可执行代码,另一个指向设置调用所需的数据(例如,'this' 指针,在绑定成员的情况下)功能).

                  If you think about it, there isn't any magic solution to this. A C-style callback is stored as a single pointer which points to executable code. Any nontrivial boost::function<> is going to need at least two pointers: one to the executable code, the other to the data that's needed to set up the call (e.g. the 'this' pointer, in the case of a bound member function).

                  在 C 回调中使用 boost::function 和 boost::bind 的正确方法是创建一个满足回调签名的 shim 函数,找出要调用的函数<>,然后调用它.通常,C 回调会为用户数据"提供某种 void*;这就是你隐藏函数指针的地方:

                  The right way to use boost::function and boost::bind with C callbacks is to create a shim function that satisfies the callback signature, figures out which function<> to call, and calls it. Usually C callbacks will have some kind of a void* for 'user data'; that's where you stash your function pointer:

                  typedef void (*CallbackType)(int x, void* user_data);
                  void RegisterCallback(CallbackType cb, void* user_data);
                  
                  void MyCallback(int x, void* userData) {
                    boost::function<void(int)> pfn = static_cast<boost::function<void(int)> >(userData);
                    pfn(x);
                  }
                  
                  boost::function<void(int)> fn = boost::bind(myFunction(5));
                  RegisterCallback(MyCallback, &fn);
                  

                  当然,如果您的回调签名不包含某种用户数据指针,那您就不走运了.但是任何不包含用户数据指针的回调在大多数实际场景中已经无法使用,需要重写.

                  Of course, if your callback signature doesn't include some kind of user data pointer, you're out of luck. But any callback that doesn't include a user data pointer is already unusable in most real-world scenarios, and needs to be rewritten.

                  这篇关于将 boost::function 降级为普通函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                    <tbody id='Kf7Tz'></tbody>

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

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

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

                          • <tfoot id='Kf7Tz'></tfoot>