title: C++ Friend function of nested class in class template
body: <p>I'm having the following problem. The code below runs fine on gdb online,
  however locally compiling like this:</p>
  
  <blockquote>
    <p>/.../g++ -std=c++17 -g -O3<br>
    /.../Test.cpp -o<br>
    /.../Test</p>
  </blockquote>
  
  <p>produces:</p>
  
  <blockquote>
    <p>error:<br>
    'privateMember' is a private member of '<code>Foo&lt;int&gt;::Nested</code>'</p>
  </blockquote>
  
  <p>I also tried with VS2019 - same effect as g++ (which is really a clang:
  <em>Apple clang version 11.0.0 (clang-1100.0.33.8)</em>).</p>
  
  <p><strong>What fixes this issue is:</strong> changing call (comment "//2")</p>
  
  <pre><code>make(...) -&gt; make&lt;int&gt;(...)
  </code></pre>
  
  <p><strong>or</strong> </p>
  
  <p>removing line with comment "//1". </p>
  
  <p>The fixes are independent from each other - <strong>only one needs to be applied</strong>. </p>
  
  <pre><code>template &lt;typename T&gt;
  class Foo;
  
  template &lt;typename T&gt;
  typename Foo&lt;T&gt;::Nested make(T&amp;&amp; t);
  
  template &lt;typename T&gt;
  class Foo
  {
  public:
      class Nested
      {
          int privateMember{5};
          friend Nested make&lt;T&gt;(T&amp;&amp; e);
      };
  
      Nested k; //1
  };
  
  template &lt;typename T&gt;
  typename Foo&lt;T&gt;::Nested make(T&amp;&amp; t)
  {
      typename Foo&lt;T&gt;::Nested nested{}; 
      std::cout&lt;&lt;nested.privateMember&lt;&lt;std::endl;
      return nested;
  }
  
  int main() {
      auto i = make(1); //2
      return 0; }
  </code></pre>
  
  <p><strong>Can somebody explain to me what is happening here?</strong> Why the code runs fine on gbd online and not with g++, also why the changes (1, 2) help?</p>