title: Passing std::array as arguments of template variadic function
body: <p>I am trying to learn about variadic templates in C++11. I have a class which is basically a wrapper around a <code>std::array</code>. I want to be able to pass function objects (ideally lambdas) to a member function and then have the elements of the <code>std::array</code> passed on as parameters of the function object.</p>
  
  <p>I have used a <code>static_assert</code> to check that the number of parameters matches the length of the array but I cannot think of a way to pass the elements as arguments.</p>
  
  <p>Here is the code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;array&gt;
  #include &lt;memory&gt;
  #include &lt;initializer_list&gt;
  
  using namespace std;
  
  template&lt;int N, typename T&gt;
  struct Container {
      template&lt;typename... Ts&gt;
      Container(Ts&amp;&amp;... vs) : data{{std::forward&lt;Ts&gt;(vs)...}} {
          static_assert(sizeof...(Ts)==N,"Not enough args supplied!");
      }
  
      template&lt; typename... Ts&gt;
      void doOperation( std::function&lt;void(Ts...)&gt;&amp;&amp; func )
      {
          static_assert(sizeof...(Ts)==N,"Size of variadic template args does not match array length");
  
          // how can one call func with the entries
          // of data as the parameters (in a way generic with N)
      }
  
      std::array&lt;T,N&gt; data;
  };
  
  int main(void)
  {
      Container&lt;3,int&gt; cont(1,2,3);
  
      double sum = 0.0;
      auto func = [&amp;sum](int x, int y, int z)-&gt;void{
          sum += x;
          sum += y;
          sum += z;
      };
  
      cont.doOperation(std::function&lt;void(int,int,int)&gt;(func));
  
      cout &lt;&lt; sum &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>So my question (as indicated in the code) is how can one pass the entries of <code>data</code> onto the function <code>func</code> in a way which is generic with <code>N</code>?</p>
  
  <p><strong>Bonus Question:</strong> Is it possible to do away with the unsightly conversion to <code>std::function</code> in main and pass in a lambda directly?</p>