title: using extern template (C++11)
body: <h2>Figure 1: <em>function templates</em></h2>
  
  <p>TemplHeader.h</p>
  
  <pre><code>template&lt;typename T&gt;
  void f();
  </code></pre>
  
  <p>TemplCpp.cpp</p>
  
  <pre><code>template&lt;typename T&gt;
  void f(){
     //...
  }    
  //explicit instantation
  template void f&lt;T&gt;();
  </code></pre>
  
  <p>Main.cpp</p>
  
  <pre><code>#include "TemplHeader.h"
  extern template void f&lt;T&gt;(); //is this correct?
  int main() {
      f&lt;char&gt;();
      return 0;
  }
  </code></pre>
  
  <p>Is this the correct way to use <code>extern template</code>, or do I use this keyword only for class templates as in Figure 2?</p>
  
  <h2>Figure 2: <em>class templates</em></h2>
  
  <p>TemplHeader.h</p>
  
  <pre><code>template&lt;typename T&gt;
  class foo {
      T f();
  };
  </code></pre>
  
  <p>TemplCpp.cpp</p>
  
  <pre><code>template&lt;typename T&gt;
  void foo&lt;T&gt;::f() {
      //...
  }
  //explicit instantation
  template class foo&lt;int&gt;;
  </code></pre>
  
  <p>Main.cpp</p>
  
  <pre><code>#include "TemplHeader.h"
  extern template class foo&lt;int&gt;();
  int main() {
      foo&lt;int&gt; test;
      return 0;
  }
  </code></pre>
  
  <p>I know it is good to put all of this in one header file, but if we instantiate templates with the same parameters in multiple files, then we got multiple same definitions and the compiler will remove them all (except one) to avoid errors. How do I use <code>extern template</code>? Can we use it only for classes, or can we use it for functions too?</p>
  
  <p>Also, Figure 1 and Figure 2 may be expanded to a solution where templates are in a single header file . In that case, we need to use the <code>extern template</code> keyword to avoid multiple same instantations. Is this only for classes or functions too?</p>