title: Template Linking and Separation of .h and .cc Files
body: <p>I've been doing some reading into designing template code have a question about it.  Most of the solutions to problems relating to designing code as templates seem to either be:</p>
  
  <ol>
  <li>Put definitions of prototypes into the header file</li>
  <li>Use the export keyword <a href="http://www.parashift.com/c++-faq-lite/templates.html#faq-35.14" rel="nofollow">like this</a> (which requires an extra compiler option)</li>
  <li>Specifically lay out how the templates will be used in the .cc/.cpp file.</li>
  </ol>
  
  <p>For example:</p>
  
  <pre><code>// File: foo_impl.cc
  // We're working with Class Foo
  #include "foo.cc"
  
  template class Foo &lt;int&gt;;
  template class Foo &lt;string&gt;;
  // etc.
  </code></pre>
  
  <p>None of these methods seem very effective.  Unless if I'm missing something, they don't seem to offer the ability for a user to simply import the header file and link the template code (in a .cc file) without doing extra work.  I was wondering if people could take a look at what I'm doing with my own code and tell me if these violate some kind of best practices protocol or if they could cause an issue that I'm just not seeing.  Here's what I've been doing...</p>
  
  <p>In main.cc:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "foo.h"
  
  using namespace std;
  
  int main (void) {
     Foo &lt;string&gt; f ("hello world");
     string s = f.get ();
     cout &lt;&lt; s &lt;&lt; endl;
  
     return 0;
  }
  </code></pre>
  
  <p>In foo.h:</p>
  
  <pre><code>#ifndef FOO_H
  #define FOO_H
  
  template &lt;class T&gt;
  class Foo {
     public:
        Foo (T);
        T get ();
  
     private:
        T data;
  };
  
  #endif
  
  #include "foo.cc"
  </code></pre>
  
  <p>In foo.cc:</p>
  
  <pre><code>#ifndef FOO_CC
  #define FOO_CC
  
  #include "foo.h"
  
  template &lt;class T&gt;
  Foo :: Foo (T stuff) {
     data = stuff;
  }
  
  template &lt;class T&gt;
  T Foo &lt;T&gt; :: get () {
     return data;
  }
  
  #endif
  </code></pre>
  
  <p>I've been able to compile the code with all warnings in gcc 4.1.2.  Thank you!</p>