title: Storing C++ template function definitions in a .CPP file
body: <p>I have some template code that I would prefer to have stored in a CPP file instead of inline in the header.  I know this can be done as long as you know which template types will be used.  For example:</p>
  
  <p><strong>.h file</strong></p>
  
  <pre><code>class foo
  {
  public:
      template &lt;typename T&gt;
      void do(const T&amp; t);
  };
  </code></pre>
  
  <p><strong>.cpp file</strong></p>
  
  <pre><code>template &lt;typename T&gt;
  void foo::do(const T&amp; t)
  {
      // Do something with t
  }
  
  template void foo::do&lt;int&gt;(const int&amp;);
  template void foo::do&lt;std::string&gt;(const std::string&amp;);
  </code></pre>
  
  <p>Note the last two lines - the foo::do template function is only used with ints and std::strings, so those definitions mean the app will link.</p>
  
  <p>My question is - is this a nasty hack or will this work with other compilers/linkers? I am only using this code with VS2008 at the moment but will be wanting to port to other environments.</p>