title: How to declare a variadic template function *instance* as a friend
body: <p>My issue <em>extends</em> this one: <a href="https://stackoverflow.com/questions/21793092/how-to-declare-a-variadic-template-function-as-a-friend">How to declare a variadic template function as a friend?</a></p>
  
  <p>I know how to make a template variadic function a friend of a class
  for <em>all</em> instantiations of template arguments.
  But I did not figured out how to make friends from only some explicitly listed instantiations.</p>
  
  <p>Example:</p>
  
  <pre><code>template &lt;typename T, typename... Args&gt;
  T create(Args&amp;&amp;... args)
  {
      T t(forward&lt;Args&gt;(args)...);
      // ...
      return t;
  }
  
  class A {
  public:
      /* -- This would allow to use all versions of constructors ..
      template &lt;typename T, typename... Args&gt;
      friend T create(Args&amp;&amp;... args);
      */
      // How to make these work?
      friend A create&lt;A&gt;(int);
      friend A create&lt;A&gt;(int, int);
      // .. is replacing T by A necessary at all?
  protected:
      A() = default;  // &lt; this should 'stay protected'!
      A(int) { }
      A(int, int) { }
  };
  </code></pre>
  
  <p>The goal is to make friends from only some versions of <code>create</code>,
  which means that I want only some versions of constructors
  to be accessible in public, but others not.</p>
  
  <p>I also want friend <code>create</code>s to stick only to <code>A</code> return types (and constructors), but maybe it is not necessary at all since friends are not inheriting and other constructors than <code>A</code> may not be influenced inside <code>A</code>?</p>
  
  <p>The solution can require features from up to C++14, but preferably not from newer standards.</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>If I needed only elementary types like <code>int</code> then @ritesh solution would be completely satisfactory as copying elements is equally efficient as moving.
  However, my use case is actually different and needs to avoid unnecessary copying of more complex objects, and constructors can have arguments that are passed by value, either as a copy or moved into (I can't remember how this idiom is called .. see the example below).</p>
  
  <p>I've done this solution, which uses (and checks) forward references as @Jarod42 came up with. I used a helper struct <code>E</code> to demonstrate copying and moving its objects.</p>
  
  <pre><code>template &lt;typename T, typename... Args&gt;
  T create(Args&amp;&amp;... args)
  /** ++ This would have copy E even if it is not necessary ..
  T create(Args... args)
  */
  {
      T t(forward&lt;Args&gt;(args)...);
      // ...
      return t;
  }
  
  struct E {
      E() { cout &lt;&lt; "E()" &lt;&lt; endl; }
      E(const E&amp;) { cout &lt;&lt; "E(const E&amp;)" &lt;&lt; endl; }
      E&amp; operator =(const E&amp;) { cout &lt;&lt; "= const E&amp;" &lt;&lt; endl; return *this; }
      E(E&amp;&amp;) { cout &lt;&lt; "E(E&amp;&amp;)" &lt;&lt; endl; }
      E&amp; operator =(E&amp;&amp;) { cout &lt;&lt; "= E&amp;&amp;" &lt;&lt; endl; return *this; }
  };
  
  class A {
  public:
      /** -- This would allow to use all versions of constructors ..
      template &lt;typename T, typename... Args&gt;
      friend T create(Args&amp;&amp;... args);
      */
  
      friend A create&lt;A&gt;(const E&amp;);
      friend A create&lt;A&gt;(E&amp;);
      friend A create&lt;A&gt;(E&amp;&amp;);
      friend A create&lt;A&gt;(const E&amp;, const E&amp;);
      friend A create&lt;A&gt;(const E&amp;, E&amp;);
      friend A create&lt;A&gt;(const E&amp;, E&amp;&amp;);
      friend A create&lt;A&gt;(E&amp;, const E&amp;);
      friend A create&lt;A&gt;(E&amp;, E&amp;);
      friend A create&lt;A&gt;(E&amp;, E&amp;&amp;);
      friend A create&lt;A&gt;(E&amp;&amp;, const E&amp;);
      friend A create&lt;A&gt;(E&amp;&amp;, E&amp;);
      friend A create&lt;A&gt;(E&amp;&amp;, E&amp;&amp;);
  
      /** ++ This would have copy E even if it is not necessary ..
      friend A create&lt;A&gt;(E);
      friend A create&lt;A&gt;(E, E);
      */
  protected:
      A() = default;  //&lt; this should 'stay protected'!
      A(E) { }  //&lt; either copy or move E
      A(const E&amp;, E) { }  //&lt; must not copy the first argument
  };
  </code></pre>
  
  <p>Luckily, it is sufficient to declare as friends only variants with reference argument types (<code>const E&amp;</code>, <code>E&amp;</code>, <code>E&amp;&amp;</code>), but not <code>E</code>: this is what <code>forward</code> will take care of.</p>
  
  <p>Test:</p>
  
  <pre><code>E e;
  const E ce;
  
  cout &lt;&lt; endl &lt;&lt; "(E) &lt;- (e) ..." &lt;&lt; endl;
  create&lt;A&gt;(e);
  cout &lt;&lt; endl &lt;&lt; "(E) &lt;- (ce) ..." &lt;&lt; endl;
  create&lt;A&gt;(ce);
  cout &lt;&lt; endl &lt;&lt; "(E) &lt;- (E()) ..." &lt;&lt; endl;
  create&lt;A&gt;(E());
  
  cout &lt;&lt; endl &lt;&lt; "(const E&amp;, E) &lt;- (e, e) ..." &lt;&lt; endl;
  create&lt;A&gt;(e, e);
  cout &lt;&lt; endl &lt;&lt; "(const E&amp;, E) &lt;- (ce, ce) ..." &lt;&lt; endl;
  create&lt;A&gt;(ce, ce);
  cout &lt;&lt; endl &lt;&lt; "(const E&amp;, E) &lt;- (ce, e) ..." &lt;&lt; endl;
  create&lt;A&gt;(ce, e);
  cout &lt;&lt; endl &lt;&lt; "(const E&amp;, E) &lt;- (ce, E()) ..." &lt;&lt; endl;
  create&lt;A&gt;(ce, E());
  cout &lt;&lt; endl &lt;&lt; "(const E&amp;, E) &lt;- (move(e), E()) ..." &lt;&lt; endl;
  create&lt;A&gt;(move(e), E());
  </code></pre>
  
  <p>.. will produce:</p>
  
  <pre><code>E()
  E()
  
  (E) &lt;- (e) ...
  E(const E&amp;)
  
  (E) &lt;- (ce) ...
  E(const E&amp;)
  
  (E) &lt;- (E()) ...
  E()
  E(E&amp;&amp;)
  
  (const E&amp;, E) &lt;- (e, e) ...
  E(const E&amp;)
  
  (const E&amp;, E) &lt;- (ce, ce) ...
  E(const E&amp;)
  
  (const E&amp;, E) &lt;- (ce, e) ...
  E(const E&amp;)
  
  (const E&amp;, E) &lt;- (ce, E()) ...
  E()
  E(E&amp;&amp;)
  
  (const E&amp;, E) &lt;- (move(e), E()) ...
  E()
  E(E&amp;&amp;)
  </code></pre>
  
  <p>I believe now that this solution works, it's only a little uncomfortable since it is necessary to declare all combinations of references (including <code>E&amp;</code>, otherwise passing <code>e</code> will not work).
  If anyone has an idea how to shorten this (maybe by utilizing templates, but not macros, I seek to avoid using them), please let me know.</p>