title: Reduce compilation times and dependencies using friend class
body: <p>In one of my projects I have a tree data structure, that might contain values of a generic type <code>T</code>.</p>
  
  <p>In order to reduce compilation times and dependencies I tried to move implementation details from the tree node class <code>Node</code> to class <code>NodeImpl</code>. The internals of <code>Node</code> are private and should be only accessed at defined entry points, for example by the <code>getName</code> function.</p>
  
  <p>Only people that are interested in using <code>getName</code> should include <code>NodeImpl</code> in their source files. This is how I think to reduce my compilation times and dependencies.</p>
  
  <p>But for some reason the following three toys classes will not compile. It says no access to private members. What I'm doing wrong?</p>
  
  <p>File <code>main.cpp</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Node.h"
  #include "NodeImpl.h"
  
  int main(int argc, char** args) {
      Node&lt;int&gt; n("Test", 2);
      std::cout &lt;&lt; getName(n) &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>File <code>Node.h</code>:</p>
  
  <pre><code>#pragma once
  #include &lt;string&gt;
  
  template&lt;typename T&gt;
  class NodeImpl;
  
  template&lt;typename T&gt;
  class Node {
  public:
      typedef T value_type;
      Node(const std::string&amp; name, const T&amp; value) : name(name), value(value) {}
  
  private:
      std::string name;
      T value;
      friend class NodeImpl&lt;T&gt;;
  };
  </code></pre>
  
  <p>File <code>NodeImpl.h</code>:</p>
  
  <pre><code>#pragma once
  #include "Node.h"
  
  template&lt;typename T&gt;
  std::string getName(Node&lt;T&gt;&amp; n);
  
  template&lt;typename T&gt;
  class NodeImpl {
      NodeImpl(Node&lt;T&gt;&amp; node) : mNode(node) {
      }
      Node&lt;T&gt;&amp; mNode;
  
      std::string name() {
          return mNode.name;
      }
  
      friend std::string getName(Node&lt;T&gt;&amp; n);
  };
  
  template&lt;typename T&gt;
  std::string getName(Node&lt;T&gt;&amp; n) {
      auto x = NodeImpl&lt;T&gt;(n);
      return x.name();
  }
  </code></pre>