title: Function pointer with variadic template arguments
body: <p>Referring to the code below, can someone figure out how to adapt</p>
  
  <pre><code>template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
  RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&amp;&amp;... args) {
      const std::tuple&lt;ARGS2...&gt; t(args...);
      for (Object* x : objects)
          (x-&gt;*f)(std::get&lt;0&gt;(t), o-&gt;rating, std::get&lt;1&gt;(t), o-&gt;str);
  }
  </code></pre>
  
  <p>so that I don't have to rewrite different versions of this every time ARGS2... is to be changed.  I don't mind doing it in the case where the argument consists of only 4 parameters, but you can imagine that the generalization would be needed if it was much greater than 4.  The types in ARGS1... shall consist of different types, so there should be a way get std::get&lt;0>(t), std::get&lt;1>(t), ... correctly placed so that there is no need to do it manually like above (even if there were duplicate types, then they can simply be placed in the first slot of the duplicate types).   Here is the full code below (the context is that as each Object subscriber to a Mediator changes, the other Object subscribers to the Mediator shall change accordingly):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;tuple&gt;
  
  struct Mediator {
      std::vector&lt;struct Object*&gt; objects;
  
      void registerObject (Object* o) {objects.emplace_back(o);}
  
      template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
      RET change (Object*, RET (Object::*)(ARGS1...), ARGS2&amp;&amp;...);
  };
  
  struct Object {
      int value;
      double rating;
      char letter;
      std::string str;
      Mediator&amp; mediator;
  
      Object (int v, double r, char l, const std::string&amp; s, Mediator&amp; m) :
      value(v), rating(r), letter(l), str(s), mediator(m) {mediator.registerObject(this);}
  
      virtual void adjust (int, double, char, const std::string&amp;) = 0;
  
      template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
      RET change (RET (Object::*f)(ARGS1...), ARGS2&amp;&amp;... args) {
          return mediator.change(this, f, std::forward&lt;ARGS2&gt;(args)...);
      }
  };
  
  struct A : Object {
      using Object::Object;
      virtual void adjust (int a, double b, char c, const std::string&amp; s) override {
          std::cout &lt;&lt; "Type A adjusted using values " &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", and " &lt;&lt; s &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  
  struct B : Object {
      using Object::Object;
      virtual void adjust (int a, double b, char c, const std::string&amp; s) override {
          std::cout &lt;&lt; "Type B adjusted using values " &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", and " &lt;&lt; s &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  
  struct C : Object {
      using Object::Object;
      virtual void adjust (int a, double b, char c, const std::string&amp; s) override {
          std::cout &lt;&lt; "Type C adjusted using values " &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", and " &lt;&lt; s &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  
  template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
  RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&amp;&amp;... args) {
      const std::tuple&lt;ARGS2...&gt; t(args...);
      for (Object* x : objects)
          (x-&gt;*f)(std::get&lt;0&gt;(t), o-&gt;rating, std::get&lt;1&gt;(t), o-&gt;str);
  }
  
  int main() {
      Mediator mediator;
      Object *a = new A(6, 1.2, 'a', "alan", mediator);
      Object *b = new B(2, 6.5, 'b', "bob", mediator);
      Object *c = new C(4, 0.8, 'c', "craig", mediator);
  
      c-&gt;change (&amp;Object::adjust, 8, 'k');
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Type A adjusted using values 8, 0.8, k, and craig.
  Type B adjusted using values 8, 0.8, k, and craig.
  Type C adjusted using values 8, 0.8, k, and craig.
  </code></pre>
  
  <p>This is as far as I've gotten with my solution.  It gives the same output, but the line marked <code>// Here!</code> is what I need generated automatically.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;tuple&gt;
  
  template &lt;std::size_t...&gt; struct index_sequence {};
  
  template &lt;std::size_t N, std::size_t... Is&gt;
  struct make_index_sequence_helper : make_index_sequence_helper&lt;N-1, N-1, Is...&gt; {};
  
  template &lt;std::size_t... Is&gt;
  struct make_index_sequence_helper&lt;0, Is...&gt; {
      using type = index_sequence&lt;Is...&gt;;
  };
  
  template &lt;std::size_t N&gt;
  using make_index_sequence = typename make_index_sequence_helper&lt;N&gt;::type;
  
  struct Mediator {
      std::vector&lt;struct Object*&gt; objects;
  
      void registerObject (Object* o) {objects.emplace_back(o);}
  
      template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
      RET change (Object*, RET (Object::*)(ARGS1...), ARGS2&amp;&amp;...);
  
      template &lt;typename RET, typename... ARGS, std::size_t... Is&gt;
      RET changeHelper (RET (Object::*)(ARGS...), const std::tuple&lt;ARGS...&gt;&amp;, index_sequence&lt;Is...&gt;);
  };
  
  struct Object {
      int value;
      double rating;
      char letter;
      std::string str;
      Mediator&amp; mediator;
  
      Object (int v, double r, char l, const std::string&amp; s, Mediator&amp; m) :
      value(v), rating(r), letter(l), str(s), mediator(m) {mediator.registerObject(this);}
  
      virtual void adjust (int, double, char, const std::string&amp;) = 0;
  
      template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
      RET change (RET (Object::*f)(ARGS1...), ARGS2&amp;&amp;... args) {
          return mediator.change(this, f, std::forward&lt;ARGS2&gt;(args)...);
      }
  };
  
  struct A : Object {
      using Object::Object;
      virtual void adjust (int a, double b, char c, const std::string&amp; s) override {
          std::cout &lt;&lt; "Type A adjusted using values " &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", and " &lt;&lt; s &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  
  struct B : Object {
      using Object::Object;
      virtual void adjust (int a, double b, char c, const std::string&amp; s) override {
          std::cout &lt;&lt; "Type B adjusted using values " &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", and " &lt;&lt; s &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  
  struct C : Object {
      using Object::Object;
      virtual void adjust (int a, double b, char c, const std::string&amp; s) override {
          std::cout &lt;&lt; "Type C adjusted using values " &lt;&lt; a &lt;&lt; ", " &lt;&lt; b &lt;&lt; ", " &lt;&lt; c &lt;&lt; ", and " &lt;&lt; s &lt;&lt; "." &lt;&lt; std::endl;
      }
  };
  
  template &lt;typename RET, typename... ARGS1, typename... ARGS2&gt;
  RET Mediator::change (Object* o, RET (Object::*f)(ARGS1...), ARGS2&amp;&amp;... args) {
      const std::tuple&lt;ARGS2...&gt; t(args...);
        // Here!
      const std::tuple&lt;ARGS1...&gt; tuple(std::get&lt;0&gt;(t), o-&gt;rating, std::get&lt;1&gt;(t), o-&gt;str);
      changeHelper (f, tuple, make_index_sequence&lt;sizeof...(ARGS1)&gt;());
  }
  
  template &lt;typename RET, typename... ARGS, std::size_t... Is&gt;
  RET Mediator::changeHelper (RET (Object::*f)(ARGS...),
          const std::tuple&lt;ARGS...&gt;&amp; tuple, index_sequence&lt;Is...&gt;) {
      for (Object* x : objects)
          (x-&gt;*f) (std::get&lt;Is&gt;(tuple)...);   
  }
  
  int main() {
      Mediator mediator;
      Object *a = new A(6, 1.2, 'a', "alan", mediator);
      Object *b = new B(2, 6.5, 'b', "bob", mediator);
      Object *c = new C(4, 0.8, 'c', "craig", mediator);
  
      c-&gt;change (&amp;Object::adjust, 8, 'k');
  }
  </code></pre>
  
  <p>How to auto-generate the tuple</p>
  
  <pre><code>const std::tuple&lt;ARGS1...&gt; tuple(std::get&lt;0&gt;(t), o-&gt;rating, std::get&lt;1&gt;(t), o-&gt;str);
  </code></pre>
  
  <p>using something along the lines of</p>
  
  <pre><code>template &lt;typename... ARGS1, typename... ARGS2&gt;
  std::tuple&lt;ARGS1...&gt; extractTuple (Object* o, ARGS2&amp;&amp;... args);
  </code></pre>
  
  <p>so that new versions of Mediator::change will not be needed for different (possibly many, if ARGS1... is large) choices of ARGS2... ?  My current idea is to use a recursive helper method, std::is_same, std::tuple_cat, etc... but I'm having problems (I think we are unpacking ARGS2... within unpacking ARGS1... during the checking of types).</p>