title: "unpack" std::array<T,N> as arguments to function
body: <p>Here is quite nice (not mine) example how u can expand (or "explode") tuple as arguments to function:</p>
  
  <pre><code>template&lt;int ...I&gt; struct index_tuple_type {
    template&lt;int N&gt; using append = index_tuple_type&lt;I..., N&gt;;
  };
  
  template&lt;int N&gt; struct make_index_impl {
    using type = typename make_index_impl&lt;N-1&gt;::type::template append&lt;N-1&gt;;
  };
  
  template&lt;&gt; struct make_index_impl&lt;0&gt; { using type = index_tuple_type&lt;&gt;; };
  
  template&lt;int N&gt; using index_tuple = typename make_index_impl&lt;N&gt;::type;
  
  template &lt;typename I, typename ...Args&gt;
  struct func_traits;
  
  template &lt;typename R, int ...I, typename ...Args&gt;
  struct func_traits&lt;R, index_tuple_type&lt;I...&gt;, Args...&gt; {
    template &lt;typename TT, typename FT&gt;
    static inline R call(TT &amp;&amp;t, FT &amp;&amp;f) {
      return f(std::get&lt;I&gt;(std::forward&lt;TT&gt;(t))...);
    }
  };
  
  template&lt;
    typename FT,
    typename ...Args, 
    typename R = typename std::result_of&lt;FT(Args&amp;&amp;...)&gt;::type
  &gt;
  inline R explode(std::tuple&lt;Args...&gt;&amp; t, FT &amp;&amp;f) {
    return func_traits&lt;R, index_tuple&lt;sizeof...(Args)&gt;, Args...&gt;
      ::call(t, std::forward&lt;FT&gt;(f));
  }
  </code></pre>
  
  <p>then you can use this like so:</p>
  
  <pre><code>void test1(int i, char c) {
    printf("%d %c\n", i, c);
  }
  
  int main() {
    std::tuple&lt;int, char&gt; t1{57, 'a'};
    explode(t1, test1);
  }
  </code></pre>
  
  <p><a href="http://ideone.com/abiKOo" rel="nofollow">Live version</a></p>
  
  <p>I was wandering how could you do the same thing with <code>std::array</code> since it quite like tuple. <code>std::get&lt;N&gt;</code> works with <code>std::array</code> so I thought that it would be easy to modify this solution. But something like this doesn't work:</p>
  
  <pre><code>template&lt;
    typename FT,
    typename Arg,
    std::size_t I,
    typename R = typename std::result_of&lt;FT(Arg&amp;&amp;)&gt;::type
  &gt;
  inline R explode(std::array&lt;Arg, I&gt;&amp; t, FT &amp;&amp;f) {
    return func_traits&lt;R, index_tuple&lt;I&gt;, Arg&gt;::
      call(t, std::forward&lt;FT&gt;(f));
  }
  
  void test2(int i1, int i2) {
    printf("%d %d\n", i1, i2);
  }
  
  int main() {
    std::array&lt;int, int&gt; t1{1, 2};
    explode(t2, test1);
  }
  </code></pre>
  
  <p>because of the part <code>std::result_of&lt;FT(Arg&amp;&amp;)&gt;::type</code>. The argument type <code>Arg&amp;&amp;</code> is wrong and <code>result_of</code> has no field <code>type</code>. For tuple <code>Args&amp;&amp;...</code> expanded, but now it should be "repeated" <code>I</code> times. Is there a way to do this using <code>result_of</code> so the returned type can be deducted?</p>
  
  <p>Also i was wondering, having the tools to "unpack" <code>tuple</code> and <code>array</code> would it be possible to "unpack" recursively (probably using <code>enable_if</code>) structure like <code>tuple&lt;array&lt;int, 2&gt;, tuple&lt;array&lt;double,3&gt;, ...</code> and so on? Some kind of a tree where <code>tuple</code> and <code>array</code> are branches, and other types are leaves? </p>