title: What's the best way to wrap a C callback with a C++11 interface?
body: <p>Let's say this is a C function to be wrapped:</p>
  
  <pre><code>void foo(int(__stdcall *callback)());
  </code></pre>
  
  <p>The two main pitfalls with C function pointer callbacks are:</p>
  
  <ul>
  <li>Not being able to store bind expressions </li>
  <li>Not being able to store capturing lambdas </li>
  </ul>
  
  <p>I would like to know the best way to wrap functions like these to do so. The first is particularly useful for a member function callback, and the second for an inline definition that uses surrounding variables, but those are not the only uses.</p>
  
  <p>The other property of these particular function pointers is that they need to use the <code>__stdcall</code> calling convention. This, to my knowledge, eliminates lambdas as an option completely, and is a bit of a nuisance otherwise. I'd like to allow at least <code>__cdecl</code> as well.</p>
  
  <p>This is the best I am able to come up with without things starting to bend back to relying on support that function pointers don't have. It would typically be in a header. Here is the following example on <a href="http://coliru.stacked-crooked.com/view?id=d3222865b8c912f23dbe130e9a8bb6d3-3afcc3bc1f2cd10b247c33a6cde9edad">Coliru</a>.</p>
  
  <pre><code>#include &lt;functional&gt;
  
  //C function in another header I have no control over
  extern "C" void foo(int(__stdcall *callback)()) {
      callback();
  }
  
  namespace detail {
      std::function&lt;int()&gt; callback; //pretend extern and defined in cpp
  
      //compatible with the API, but passes work to above variable
      extern "C" int __stdcall proxyCallback() { //pretend defined in cpp
          //possible additional processing
          return callback();
      }
  }
  
  template&lt;typename F&gt; //takes anything
  void wrappedFoo(F f) {
      detail::callback = f;
      foo(detail::proxyCallback); //call C function with proxy 
  }
  
  int main() {
      wrappedFoo([&amp;]() -&gt; int {
          return 5;
      });   
  }
  </code></pre>
  
  <p>There is, however, a major flaw. This is not re-entrant. If the variable is reassigned to before it's used, the old function will never be called (not taking into account multithreading issues).</p>
  
  <p>One thing I have tried that ended up doubling back on itself was storing the <code>std::function</code> as a data member and using objects, so each would operate on a different variable, but there was no way to pass the object to the proxy. Taking the object as a parameter would cause the signature to mismatch and binding it would not let the result be stored as a function pointer.</p>
  
  <p>One idea I have, but have not played around with is a vector of <code>std::function</code>. However, I think the only real safe time to erase from it would be to clear it when nothing is using it. However, each entry is first added in <code>wrappedFoo</code>, then used in <code>proxyCallback</code>. I'm wondering if a counter that is incremented in the former and decremented in the latter, then checked for zero before clearing the vector would work, but it sounds like a more convoluted solution than necessary anyway.</p>
  
  <p>Is there any way to wrap a C function with a function pointer callback such that the C++ wrapped version:</p>
  
  <ul>
  <li>Allows any function object</li>
  <li>Allows more than just the C callback's calling convention (if it's critical that it's the same, the user can pass in something with the right calling convention)</li>
  <li>Is thread-safe/re-entrant</li>
  </ul>
  
  <p><sub>Note: The obvious solution, stated as part of Mikael Persson's answer, is to make use of the <code>void *</code> parameter that should exist. However, this is sadly not a be-all, end-all option, mostly due to incompetence. What possibilities exist for those functions that do not have this option is where this can get interesting, and is the primary route to a very useful answer.</sub></p>