title: Central Event Dispatcher with Templated Event types in C++
body: <p>I've been learning C++ and trying to implement various idioms from other languages I'm familiar with.</p>
  
  <p>Recently I've tried to implement a central EventDispatcher which multiple objects can register events that can take N number of arguments, and a single call to EventDispatcher::dispatch(event_name) would call all registered event callbacks.</p>
  
  <p>First I created a template class for Event;</p>
  
  <pre><code>template &lt;typename ..._args&gt;
  class Event
  {
  public:
      //Alias for template callback
      using _callback = std::function&lt;void(_args...)&gt;;
  
      //Ctor &amp; Dtor
      explicit Event(std::string const&amp; name, _callback const&amp; cb) : _name(name), _cbFunc(cb) {}
      ~Event() {}
  
      //Accessors
      std::string const&amp; getName() const { return this-&gt;_name; }
  
      //Methods
      void trigger(_args... a) { this-&gt;_cbFunc(a...); }
  private:
      //Event identifier
      std::string _name;
      //Event callback - can't be changed inside event.
      _callback const _cbFunc;
  };
  </code></pre>
  
  <p>With this structure I can define events like in the following example:</p>
  
  <pre><code>void testCallback(int a) {
      std::cout &lt;&lt; a &lt;&lt; std::endl;
  }
  
  Event&lt;int&gt; _event("TestEvent", std::bind(&amp;testCallback, _1));
  _event.trigger(20); //This will actually output 20 to the console.
  </code></pre>
  
  <p>What I want to do next is, have an EventDispatcher class which should be defined like:</p>
  
  <pre><code>class EventDispatcher
  {
  public:
      EventDispatcher();
      ~EventDispatcher();
  
      void registerEvent(**Event of any type**);
      void dispatchEvent(std::string const&amp; eventName, ....args);
  private:
      std::map&lt;std::string, std::vector&lt;**Event of any type**&gt;&gt; _eventList;
  };
  </code></pre>
  
  <p>so that I can make calls like this;</p>
  
  <pre><code>EventDispatcher _dispatcher;
  _dispatcher.registerEvent(_event); //event defined in the previous example.
  
  _dispatcher.dispatchEvent("TestEvent", 20); //this should call event's trigger() method with arguments passed to it actually.
  </code></pre>
  
  <p>However I couldn't figure out how to implement a central dispatcher and have it be able to register templatized event instances, and then pass variable number of arguments to dispatchEvent(...) and make it trigger all events in a vector of templatized events.</p>
  
  <p>How can I achieve such functionality? Is my thought proccess correct or far from c++ way of implementing such a system? Any tips would be welcome.</p>