title: Store a function with arbitrary arguments and placeholders in a class and call it later
body: <p>So I am creating a type of event handler and I am in the process of writing an "Event Listener Wrapper", if you will. </p>
  
  <p>The basic idea is this:
  When you want to subscribe to an event, you create a function that should be called when the event fires. &lt;-- already have that done (kinda, I'll explain)</p>
  
  <p>You put this listener function into a wrapper to pass the function onto the dispatcher. </p>
  
  <p>The dispatcher gets an event, finds the wrapper for you listener, and calls the underlying function with the parameter values set by the event.</p>
  
  <p>I already have something working so long as the listeners all only accept one argument of my <code>EventBase</code> class. Then I have to type cast that into the proper event that the listener is passed.</p>
  
  <p>What I want instead is for my listener functions to have "any" type of arguments, and store the function in a way that lets me call it with any arguments I want depending on the event fired. Each listener function would only ever receive one type of event, or the event it's self. This would allow me to not have to type cast each event in every listener, but instead the correct event would be passed.</p>
  
  <p>I found a bit of code for this wrapper that is <em>almost</em> perfect, with a few minor issues that I can't seem to fix. I'll explain below.</p>
  
  <p>Code by @hmjd:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;functional&gt;
  #include &lt;memory&gt;
  
  void myFunc1(int arg1, float arg2)
  {
      std::cout &lt;&lt; arg1 &lt;&lt; ", " &lt;&lt; arg2 &lt;&lt; '\n';
  }
  void myFunc2(const char *arg1)
  {
      std::cout &lt;&lt; arg1 &lt;&lt; '\n';
  }
  
  class DelayedCaller
  {
  public:
      template &lt;typename TFunction, typename... TArgs&gt;
      static std::unique_ptr&lt;DelayedCaller&gt; setup(TFunction&amp;&amp; a_func,
                                                  TArgs&amp;&amp;... a_args)
      {
          return std::unique_ptr&lt;DelayedCaller&gt;(new DelayedCaller(
              std::bind(std::forward&lt;TFunction&gt;(a_func),
                        std::forward&lt;TArgs&gt;(a_args)...)));
      }
      void call() const { func_(); }
  
  private:
      using func_type = std::function&lt;void()&gt;;
      DelayedCaller(func_type&amp;&amp; a_ft) : func_(std::forward&lt;func_type&gt;(a_ft)) {}
      func_type func_;
  };
  
  int main()
  {
      auto caller1(DelayedCaller::setup(&amp;myFunc1, 123, 45.6));
      auto caller2(DelayedCaller::setup(&amp;myFunc2, "A string"));
  
      caller1-&gt;call();
      caller2-&gt;call();
  
      return 0;
  }
  </code></pre>
  
  <p>The first thing I did here was I had to replace <code>std::unique_ptr</code> with <code>std::shared_ptr</code>. Not sure why really. This almost works. In my use case, I need to store a method function (meaning bind needs to be passed the containing method object?), and at the time of storing the function I don't know what the argument value will be, thats up for the event to decide. So my adjustment is as follows:</p>
  
  <pre><code>class DelayedCaller
  {
  public:
  
      template &lt;typename TFunction, typename TClass&gt;
      static std::shared_ptr&lt;DelayedCaller&gt; setup(TFunction&amp;&amp; a_func,
                                                  TClass &amp;&amp; a_class)
      {
  
          auto func = std::bind(std::forward&lt;TFunction&gt;(a_func),
                                std::forward&lt;TClass&gt;(a_class),
                                std::placeholders::_1);
  
          return std::shared_ptr&lt;DelayedCaller&gt;(new DelayedCaller(func));
      }
  
      template &lt;typename T&gt;
      void call( T v ) const { func_(v); }
  
  private:
      using func_type = std::function&lt;void(  )&gt;;
      DelayedCaller(func_type&amp;&amp; a_ft) : func_(std::forward&lt;func_type&gt;(a_ft)) {}
      func_type func_;
  };
  </code></pre>
  
  <p>For the sake of testing, I removed the parameter pack and replaced it with a direct parameter to the class object holding the function. I also gave the bind a placeholder for 1 argument (ideally replaced by the <code>void call()</code> function later).</p>
  
  <p>It's created like this:</p>
  
  <pre><code>eventManager-&gt;subscribe(EventDemo::descriptor, DelayedCaller::setup(
                                  &amp;AppBaseLogic::getValueBasic,
                                  this
                                  ));
  </code></pre>
  
  <p>Problem is: on this line:</p>
  
  <pre><code>return std::shared_ptr&lt;DelayedCaller&gt;(new DelayedCaller(func));
  </code></pre>
  
  <p>I get "no matching function for call to 'DelayedCaller::DelayedCaller(std::_Bind(AppBaseLogic*, std::_Placeholder&lt;1>)>&amp;)'
           return std::shared_ptr(new DelayedCaller(func));"</p>
  
  <p>This only happens when using the <code>placeholder::_1</code>. if I replace that with a known value of the correct type, it works, with the exception that the function gets called without any useful data of course. </p>
  
  <p>So, I guess I need a way to store the function with placeholders that I don't know the type of?</p>
  
  <p>Forgive me if I am getting names of things wrong. I am very new to c++, I have only started learning it the past few days.</p>
  
  <p>**Edit: **</p>
  
  <p>Ok, so I am just updating why I need to store functions like this.
  I have a map in my event dispatcher that looks like this:</p>
  
  <pre><code>std::map&lt; const char*, std::vector&lt;DelayedCaller&gt; &gt; _observers;
  </code></pre>
  
  <p>I want to be able to call the function inside the "Delayed Caller" something like this:</p>
  
  <pre><code>void Dispatcher::post( const EventBase&amp; event ) const
  {
      // Side Note: I had to do this instead of map.find() and map.at() because 
      // passing a "const char*" was not evaluating as equal to event.type() even 
      // though event.type() is also a const char*. So instead I am checking it 
      // myself, which is fine because it gives me a little more control.
  
      std::string type(event.type());
      for( auto const &amp;x : _observers ) {
          std::string type2(x.first);
          if ( type == type2 ) {
              auto&amp;&amp; observers = x.second;
  
              for( auto&amp;&amp; observer : observers ) {
                  // event may be any descendant of EventBase.
                  observer.slot-&gt;call(event);
              }
              break;
          }
      }
  }
  </code></pre>
  
  <p>My listeners currently look like this:</p>
  
  <pre><code>void AppBaseLogic::getValue(const EventBase &amp;e) {
      const EventDemo&amp; demoEvent = static_cast&lt;const EventDemo&amp;&gt;( e );
      std::cout &lt;&lt; demoEvent.type();
  }
  </code></pre>
  
  <p>I am trying to store each function so that the argument may look like this:</p>
  
  <pre><code>void AppBaseLogic::getValue(const EventAnyDescendant &amp;e) {
      std::cout &lt;&lt; e.type();
  }
  </code></pre>
  
  <p>Hopefully that helps. Thank you all for taking the time to help me with this.</p>
  
  <p>Side note on lambdas: Someone suggested them, I have know idea what they are or how to use them, but I am going to do some reaserch on them so see if that would make more sense. I am worried about maintainability with them though from what I have seen.</p>