title: How to iterate through variadic template parameters to create variable number of local variables?
body: <p>I have many functions that are very similar but run with different number and type of local objects:</p>
  
  <pre><code>template &lt;class T&gt; T* create1( const std::vector&lt;std::string&gt;&amp; names )
  {
      A a( names[0] );
      B b( names[1] );
      C c( names[2] );
  
      if ( a.valid() &amp;&amp; b.valid() &amp;&amp; c.valid() )
          return new T( a, b, c );
      else
          return NULL;
  }
  
  template &lt;class T&gt; T* create2( const std::vector&lt;std::string&gt;&amp; names )
  {
      D d( names[0] );
      E e( names[1] );
  
      if ( d.valid() &amp;&amp; e.valid() )
          return new T( d, e );
      else
          return NULL;
  }
  
  create1&lt;ABC&gt;( { "nameA", "nameB", "nameC" } );
  create2&lt;DE&gt;( { "nameD", "nameE" } );
  </code></pre>
  
  <p>Would variadic template help me achieve a refactoring of those functions as this?</p>
  
  <pre><code>template &lt;class T, typename Args...&gt; T* create()
  {
      // loop over Args and create 2 or 3 objects
      // if (....valid())
      //    return T( ... );
      // else
      //    return NULL;
  }
  
  create&lt;ABC,A,B,C&gt;( { "nameA", "nameB", "nameC" } );
  create&lt;DE,D,E&gt;( { "nameD", "nameE" } );
  </code></pre>
  
  <p>Checked <a href="https://stackoverflow.com/questions/7230621/how-can-i-iterate-over-a-packed-variadic-template-argument-list">How can I iterate over a packed variadic template argument list?</a> and <a href="https://stackoverflow.com/questions/15501322/iterating-over-variadic-templates-type-parameters">iterating over variadic template&#39;s type parameters</a> with no success. Can't see how I could create a variable number of local objects of different kind...</p>