title: Event-handling using templates
body: <p>I have been playing around with the new C++11 standard lately and decided to create a basic event-handling system. The code below provides a small example of my current implementation.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;functional&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  template &lt;typename Event&gt;
  class EventBroadcaster
  {
  public:
      typedef std::function&lt;void(const Event&amp;)&gt; Connection;
  
      void connect(Connection&amp;&amp; connection)
      {
          connections.push_back(std::move(connection));
      }
  
      void signal(const Event&amp; event)
      {
          for (const auto&amp; connection : connections)
          {
              connection(event);
          }
      }
  private:
      std::vector&lt;Connection&gt; connections;
  };
  
  struct MouseMotion
  {
      int x = 0;
      int y = 0;
  };
  
  class Input : public EventBroadcaster&lt;MouseMotion&gt;
  {
  public:
      void process()
      {
          MouseMotion mouseMotion;
          mouseMotion.x = 10;
          mouseMotion.y = 20;
          signal(mouseMotion);
      }
  };
  
  int main()
  {
      int x = 0;
      int y = 0;
  
      Input input;
  
      input.connect([&amp;](const MouseMotion&amp; e){
          x += e.x;
          y += e.y;
      });
  
      input.process();
  
      std::cout &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; std::endl; // Output: 10,20
  
      return 0;
  }
  </code></pre>
  
  <p>The above solution does work quite nicely if the <code>Input</code> class would only broadcast a single event. There might however be the case that the <code>Input</code> class would want to be able to send <code>KeyPress</code> events besides just <code>MouseMotion</code> events.</p>
  
  <p>I thought about using multiple inheritance. Making <code>Input</code> inherit both <code>EventBroadcaster&lt;MouseMotion&gt;</code> and <code>EventBroadcaster&lt;KeyPress&gt;</code>. This results in compiler errors warning of ambiguous functions. The solution provided in the following answer <a href="https://stackoverflow.com/a/3360909/1175300">Multiple Inheritance Template Class</a> does work for the protected <code>signal</code> function, but not for the public <code>connect</code> function called outside the <code>Input</code> class.</p>
  
  <p>Besides multiple inheritance I wondered if variadic-templates could help my with my problem. I have looked at (partial) template specialization and unpacking variadic-templates. But have been unable to come with an (elegant) solution.</p>
  
  <p>What would be the best way to support multiple event types?</p>