title: Variadic template constructor mixing lvalues and rvalues in a tuple
body: <p>I want to defer the instantiation of a (large) bunch of classes, among which only a small subset will be instantiated. To do that, I try to capture the arguments to constructors in a variadic class which stores them in a tuple.</p>
  
  <p>My problem is that I only succeeded in storing either references or copies. However, I need to forward both kinds to the constructors, at once, or I get scope problems.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;utility&gt;
  #include &lt;tuple&gt;
  #include &lt;vector&gt;
  
  // Interface of the classes which instanciation is being deferred.
  struct OpItf {
      virtual void operator()() = 0;
      virtual ~OpItf() {}
  };
  
  struct Operator : public OpItf {
      int&amp; _i;
      int _j;
      // Need both a lvalue reference and a rvalue.
      Operator(int&amp; i, int j) : _i(i), _j(j) {}
      void operator()() {std::cout &lt;&lt; _i &lt;&lt; " " &lt;&lt; _j &lt;&lt; std::endl;}
      virtual ~OpItf() {}
  };
  
  // The interface of the class managing the actual instanciation.
  template&lt;class Itf&gt;
  struct ForgeItf {
          virtual Itf&amp; instanciate() = 0;
          virtual ~ForgeItf() {}
  };
  
  template&lt;class Itf, class Op, typename... Args&gt;
  struct Forge : public ForgeItf&lt;Itf&gt; {
  
      std::tuple&lt;Args&amp;&amp;...&gt; _args;
      Itf* _instanciated;
  
      Forge(Args&amp;&amp;... args) :
          _args(std::forward&lt;Args&gt;(args)...),
          _instanciated(nullptr)
      { }
  
      Itf&amp; instanciate()
      {
          if(_instanciated) {
              delete _instanciated;
          }
          _instanciated = op_constructor(_args);
          return *_instanciated;
      }
  
      virtual ~Forge() { delete _instanciated; }
  
      template&lt;class T&gt;
      Op* op_constructor(T&amp; args) { return new Op(std::make_from_tuple&lt;Op&gt;(args)); }
  };
  
  // A container of forges.
  template&lt;class Itf&gt;
  struct Foundry : std::vector&lt; ForgeItf&lt;Itf&gt;* &gt; {
  
          template&lt;class Op, typename... Args&gt;
          void add(Args&amp;&amp;... args)
          {
              auto pfo = new Forge&lt;Itf,Op,Args&amp;&amp;...&gt;(std::forward&lt;Args&gt;(args)...);
              this-&gt;push_back(pfo);
          }
  
          virtual ~Foundry() { for(auto p : *this) { delete p; } }
  };
  
  int main() {
  
      Foundry&lt;OpItf&gt; foundry;
  
      int iref = 1;
  
      // Store the constructors parameters.
      for(int j=0; j&lt;3; ++j) {
          foundry.add&lt; Operator &gt;(iref,j);
      }
  
      // Change the referenced parameter before instanciations.
      iref = 2;
  
      // Actually instanciate.
      for(auto&amp; forge : foundry ) {
          auto&amp; op = forge-&gt;instanciate();
          op();
      }
  }
  </code></pre>
  
  <p>Using rvalues <code>Args...</code>, the constructors get the values, but the reference to <code>i</code> is (uncorrectly) unchanged:</p>
  
  <pre><code>1 0
  1 1
  1 2
  </code></pre>
  
  <p>Using forwarding references <code>Args&amp;&amp;...</code>, the constructors get the reference correctly, but out-of-scope copies of <code>j</code>:</p>
  
  <pre><code>2 1228009304
  2 1228009304
  2 1228009304
  </code></pre>