title: Generic object creation from command line
body: <p>I have two C++ abstract classes <code>Abs1</code> and <code>Abs2</code>. Then I have: </p>
  
  <pre><code>`A : public Abs1`
  `B : public Abs1` 
  `C : public Abs2`
  `D : public Abs2`
  </code></pre>
  
  <p>Now, I'm trying to <a href="https://stackoverflow.com/questions/41407732/command-line-arguments-for-different-constructors">create objects from command line arguments</a> and I have to do rewrite the public factory function <code>make_abstract</code> in the linked question, something like:</p>
  
  <pre><code>std::unique_ptr&lt;Abs1&gt; makeAbs1 (int argc, const char*argv[])
  {
  
      if (argc == 1) {
          return nullptr;
      }
      const std::string name = argv[1];
      if (name == "A") {
          return detail::make_abstract&lt;A, std::tuple&lt;int, std::string, int&gt;&gt;(argc, argv);
      } else if (name == "B") {
          return detail::make_abstract&lt;B, std::tuple&lt;int, int&gt;&gt;(argc, argv);
      }
  }
  
  
  
  std::unique_ptr&lt;Abs2&gt; makeAbs2 (int argc, const char*argv[])
  {
  
      if (argc == 1) {
          return nullptr;
      }
      const std::string name = argv[1];
      if (name == "C") {
          return detail::make_abstract&lt;C, std::tuple&lt;int&gt;&gt;(argc, argv);
      } else if (name == "D") {
          return detail::make_abstract&lt;D, std::tuple&lt;int, float&gt;&gt;(argc, argv);
      }
  }
  </code></pre>
  
  <p>As you can see this is terribly redundant. How can I do a generic version of this? In this version we can pass as many implemented class as we want, so the <code>if</code> cascade is not a solution. Notice that we cannot modify any of these classes.</p>
  
  <p>I was thinking that <em>maybe</em> variadic templates could help, but I can't figure out many problems:</p>
  
  <pre><code>template &lt;typename T, typename ...Ts&gt;
  std::unique_ptr&lt;T&gt; make (int argc, const char*argv[]){
    const std::string name = argv[1];
    for(Ti : Ts) //this is obviously wrong
      if(typeid(Ti).name == name)
        return detail::make_abstract&lt;T, std::tuple&lt;/*Here shoudl be different for every Ti*/&gt;&gt;(argc, argv);
  
  }
  </code></pre>