title: Casting a universal reference to a callable to void pointer and vice versa
body: <p>I need to wrap a callback mechanism in a C interface in C++ accepting callables.
  In the C interface there is a method to set the callback:</p>
  <pre><code>void c_set_callback(void(*callback)(void* context), void* context);
  </code></pre>
  <p>Here is my attempt to wrap this with a callable:</p>
  <pre><code>template&lt;typename Function&gt;
  void set_callback(Function&amp;&amp; function)
  {
      c_set_callback
          ([](void* context)
              {
                  (*reinterpret_cast&lt;typename std::decay&lt;Function&gt;::type*&gt;(context))();
              }
          , reinterpret_cast&lt;void*&gt;(&amp;function)
          );
  }
  </code></pre>
  <p>But I can't figure out the right way of casting the context for the universal reference to &quot;function&quot;. With the implementation above I get a segmentation fault when the callable is invoked from within the lambda.</p>
  <p>Please see <a href="https://onlinegdb.com/r1WeuBz08" rel="nofollow noreferrer">https://onlinegdb.com/r1WeuBz08</a> for the full code example.</p>