title: How to create a vector filled with C-Style function pointers and lambdas(with and without captures)
body: <p>Ive been learning about lambdas and function pointers lately and wanted to use them in a simple callback system. 
  The map that stores an event and all callbacks that should be called when it gets triggered looks like this:<br>
   <code>std::unordered_map&lt; sf::Event::EventType , std::vector&lt; Callback &gt; &gt; eventMap;</code>.</p>
  
  <p>I defined Callback this way 
  <code>typedef void(*Callback)(sf::Event const&amp;);</code>. My <code>registerEvent()</code> function takes an event and a Callback as arguments.<br>
  <code>registerEvent()</code>can then be called this way:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>inputHandler.registerEvent(/*Any event type*/, [](sf::Event const&amp;)
  {
      //do something
  });
  </code></pre>
  
  <p>That works fine but when I want to capture an object I get an compile error saying 
  <code>error: no matching function for call to ‘InputHandler::registerEvent(sf::Event::EventType, Button::Button(ID, Callback, InputHandler&amp;)::&lt;lambda(const sf::Event&amp;)&gt;)’</code> The code that produces that error is the following:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class Button
  {
  //
  public:
      Button(ID id, Callback callback, InputHandler&amp; inputhandler)
      {
          inputhandler.registerEvent(sf::Event::MouseButtonPressed, [this](sf::Event const&amp;)
          {
              {
                getTextureRect();  
              }
          });
      }
  };
  </code></pre>
  
  <p>It seems as if the <code>this</code> capture is changing the type of the lambda, which makes it incompatible with the <code>registerEvent()</code> function. But since lots of different classes should be able to create such lambdas and also lambdas without any capture should be contained in the vector, I don't know how to tell what type the vector should be. I heard of <code>std::function</code> but (this may sound silly) I would like to use the C-Style way in order to get a better understanding. I think <a href="https://stackoverflow.com/a/52190093/10595546">this</a> post says something about that but I don't quite understand it that well. I thought about creating two maps, one for all capture-less lambdas and one for the others. Then I could let every class inherit from a base class and make the capture type in the vector a pointer to the base class, though this is pretty complex and adds unnecessary overhead and I don't even know if this would work.
  What is the general solution for a situation like this one?</p>
  
  <p>Thanks for all replies and best regards,</p>
  
  <p>Daniel Schreiber Mendes :)</p>
  
  <p><strong><em>Edit:</em></strong><br>
  Callback now looks like this:
  <code>typedef std::function&lt; void(sf::Event const&amp;) &gt; Callback;</code>
  And when I want to use it:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>//inside the Button Constructor
  inputhandler.registerEvent(sf::Event::MouseButtonPressed, Callback([this](sf::Event const&amp; event)
          {
              //memberFunc();
          }));
  </code></pre>
  
  <p>This did not give me an error but behaved in a wrong way. It did execute the lambda, but it didn't change the class in which it was defined. So I added a line in the constructor which just prints the address of the just created object. I am creating only one Button, so there should be only one adress printed. But there were two different. That means when inserting the lambda wrapped in a std::function into a vector, a new Button object gets created. How can that be? An implicit Object creation?</p>
  
  <p>Thanks for your answers :)</p>