title: std::function vs Lambda for passing member functions
body: <p>I am creating a messaging system for a game engine, where in an end user of the engine can create a message object and pass it to a game object to be interpreted by listener objects contained components attached to the game object. if the message matches a message that the listener is listening for, then the listener should call a function pointer and pass it the message that was received. The basic structure looking something like this:</p>
  
  <pre><code>class Message
  {
      std::string message;
  };
  
  class Listener
  {
      std::string target;
      void (*fn)(Message*);
  };
  </code></pre>
  
  <p>with the game object's code to receive a message looking something like this:</p>
  
  <pre><code>//if the queue is empty then dont do anything
  if (messageQueue.empty())
  {
      return;
  }
  
  //else grab the front of the queue
  Message* newMessage = messageQueue.front();
  
  //pop our message from the queue
  messageQueue.pop();
  
  //for each component in our list
  for (std::vector&lt;Component*&gt;::iterator i = ComponentList.begin(); i &lt; ComponentList.end(); i++)
  {
      Component* itemp = *i;
      //for each message in its listener list
      for (std::vector&lt;Listener*&gt;::iterator j = itemp-&gt;Listeners.begin(); j &lt; itemp-&gt;Listeners.end(); j++)
      {
          Listener* jtemp = *j;
          //check the listener against the newMessage
          if (jtemp-&gt;name == newMessage-&gt;name)
          {
              //call jtemp's function
              jtemp-&gt;function(newMessage);
          }
      }
  }
  </code></pre>
  
  <p>the issue that I have ran into is that since the functions that need to be called by the snippet above are member functions of a component type, I cannot set <code>Listener.fn</code> to equal <code>Component::foo</code> or any other function for that matter.</p>
  
  <p>I asked around my classmates to see if they could offer any advice and the two pieces of advice I received were either use lambdas or use <code>std::function</code> to convert a member function pointer into a regular function pointer.</p>
  
  <p>I've never used a lambda function before, but after researching <a href="https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11">here</a>, if I'm grasping it correctly, it seems like I would have to write a new lambda each time I pass a message which defeats the purpose of using a system like this. And after researching <code>std::function</code> <a href="http://en.cppreference.com/w/cpp/utility/functional/function" rel="nofollow noreferrer">here</a>, <a href="https://stackoverflow.com/questions/33574920/passing-a-member-function-via-stdfunction">here</a>, and <a href="https://stackoverflow.com/questions/11352936/why-do-we-use-stdfunction-in-c-rather-than-the-original-c-function-pointer">here</a> it seems like <code>std::function</code> would give me the flexibility that I need but implementation has been giving me a large number of errors</p>
  
  <p>Am I overlooking the power of lambdas, should I be using a <code>std::function</code> in this case, and if so how can a create one that returns void and takes a message pointer?</p>