title: How do I use bind to pass a member function as a function pointer?
body: <p>I'm trying to pass a member function as a function pointer so that I don't need to rely on singletons or global functions to handle Qt messages in Qt 5. As far as I can tell my std::function is of the correct type, it has the correct signature, and bind should be allowing me to jam in the implicit <code>this</code> pointer, essentially passing off a member function as a global/un-owned function.</p>
  
  <pre><code>void ProgramMessageHandler::setAsMessageHandlerForProgram() {
      std::function&lt;void(QtMsgType, const QMessageLogContext &amp;, const QString &amp;)&gt; funcPtr;
  
      funcPtr = std::bind(handleMessages, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
  
      qInstallMessageHandler(funkPtr);
  }
  </code></pre>
  
  <p>This doesn't compile. I can succesfully create my <code>funcPtr</code> variable but passing it into the <code>qInstallMessageHandler</code> function causes the following:</p>
  
  <pre><code>log\ProgramMessageManager.cpp:16: error: cannot convert 'std::function&lt;void(QtMsgType, const QMessageLogContext&amp;, const QString&amp;)&gt;' to 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&amp;, const QString&amp;)}' for argument '1' to 'void (* qInstallMessageHandler(QtMessageHandler))(QtMsgType, const QMessageLogContext&amp;, const QString&amp;)'
   oldHandler = qInstallMessageHandler(hackedPointerToHandleFunction);
                                                                    ^
  </code></pre>
  
  <p>I have read:</p>
  
  <p><a href="https://stackoverflow.com/questions/3402195/how-to-pass-a-member-function-as-a-function-pointer">how to pass a member function as a function pointer?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/130322/how-do-you-pass-a-member-function-pointer">How do you pass a member function pointer?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26691656/how-to-pass-a-member-function-as-a-parameter-to-a-function-that-doesnt-expect-i">How to pass a member function as a parameter to a function that doesn&#39;t expect it?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10938774/get-function-pointer-from-stdfunction-when-using-stdbind">Get function pointer from std::function when using std::bind</a></p>
  
  <p>but none have helped me.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So it has been said that this isn't possible without a singleton or a global funtion... but why? The only difference between a member function and a global function with the same signature is that they <em>don't</em> have the same signature, there is an implicit <code>this</code> pointer as the first parameter to a member function.</p>
  
  <p>Knowing that, why cannot I use <code>std::bind</code> to bridge this gap, by saying 'I know that the function being called takes the <code>this</code> argument before all of the others, force it in there, be the thing that knows about <code>this</code>. That would be so much cleaner than a singleton and a global function is just crap.</p>