title: C++ class declarations and namespaces
body: <p>I'm having an issue with a C++ library I'm trying to write. It's the usual setup, one cpp file, one header file. I want the header file to only expose the parts that are meant to be used (I have an abstract base class for instance, I don't want in the header file). So far, I'm just working with a single file (I assume this should make no difference, as includes are done by the preprocessor, which doesn't care about anything).</p>
  
  <p>You'll note that the "header file" is spread over two spots, before and after the header implementation file.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  // lib.h
  namespace foo {
      template &lt;class T&gt; class A;
  }
  
  // lib.cpp
  namespace foo {
      template &lt;class T&gt; class A {
          private:
          T i;
          public:
          A(T i) {
              this-&gt;i = i;
          }
  
          T returnT() {
              return i;
          }
      };
  };
  
  // lib.h
  namespace foo {
      template &lt;class T&gt; T A&lt;T&gt;::returnT();
  }
  
  // foo.cpp
  void main() {
      foo::A&lt;int&gt; a = foo::A&lt;int&gt;(42);
      printf("a = %d",a.returnT());
  }
  </code></pre>
  
  <p>So, naturally, I'd like my header file to contain just</p>
  
  <pre><code>namespace foo {
      template &lt;class T&gt; class A;
      template &lt;class T&gt; T A&lt;T&gt;::returnT();
  }
  </code></pre>
  
  <p>But my compiler does not like this (it complains that <code>returnT</code> is not a member of <code>foo::A&lt;T&gt;</code>. The reason I don't want to put the class declaration itself in the header is that then it would (as I understand it), contain all the private and similar stuff, which I'd like to hide.</p>
  
  <p>Maybe it's just me, but the following header file seems "bad", at least as an "interface specification." It exposes some of the internals of <code>A</code>, which a user of the lib would not need to know about.</p>
  
  <pre><code>// lib.h
  namespace foo {
      template &lt;class T&gt; class A {
          private:
          int i;
          public:
          A(T);
          T returnT();
      };
  }
  
  // lib.cpp
  namespace foo {
      template &lt;class T&gt; A&lt;T&gt;::A(T i) {
          this-&gt;i = i;
      }
      template &lt;class T&gt; T A&lt;T&gt;::returnT() {
          return i;
      }
  };
  </code></pre>
  
  <p>Is this the accepted way of doing it? I'd like a more abstract header file, if at all possible.</p>