How do I pass a std::function object to a function taking a function pointer?
<p>I am trying to interface with a library written in <code>c</code>, that uses this familiar pattern:</p>

<pre><code>void some_c_handler(void(*func)(void*), void* data);
</code></pre>

<p>Now, I want to write a <code>C++</code> wrapper for this function that looks like this:</p>

<pre><code>void my_new_cpp_handler(std::function&lt;void()&gt;&amp;&amp; func)
{
  void (*p)() = foo(func);
  void* data = bar(func);
  some_c_handler(p, data);
}
</code></pre>

<p>Both <code>some_c_handler</code> and <code>my_new_cpp_handler</code> are solving the same problem; they're taking in some kind of function along with some state. But the latter is preferred in that it abstracts much of the implementation details from the user, and allows for simply passing in a lambda object.</p>

<p>So <code>my_new_cpp_handler</code> should take the <code>func</code> parameter it is given, convert it to a function pointer and pass its state on to <code>data</code>.</p>

<p>I don't know enough about the standard, or the implementation of <code>std::function</code> to know if this is even a reasonable request. Can <code>foo</code> and <code>bar</code> exist?</p>

<p>To put it differently, what I want is to be able to pass a stateful function to a <code>c</code> callback handler without having to manually instantiate my own <code>struct</code> type to pass along with it. Obviously <code>std::function</code> has already done this for us, so I'd love to be able to separate the function pointer from the state somehow and pass it onto the <code>c</code>-style handler. Is this possible?</p>