Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Has anyone noticed that the <a href="https://stackoverflow.com/a/282433/20003">accepted answer</a> only works with trivial cases? The only way that function&lt;>::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&lt;> nonsense to begin with.</p> <p>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&lt;> 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). </p> <p>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&lt;> 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:</p> <pre><code>typedef void (*CallbackType)(int x, void* user_data); void RegisterCallback(CallbackType cb, void* user_data); void MyCallback(int x, void* userData) { boost::function&lt;void(int)&gt; pfn = static_cast&lt;boost::function&lt;void(int)&gt; &gt;(userData); pfn(x); } boost::function&lt;void(int)&gt; fn = boost::bind(myFunction(5)); RegisterCallback(MyCallback, &amp;fn); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload