title: C++11: Explicit instantiation declaration vs. explicit instantiation definition
body: <p>What is the difference between the C++03's explicit template's instantiation <strong>definition</strong> and C++11's explicit template's instantiation <strong>declaration</strong> ?</p>
  
  <p>What I mean is why the instantiation definition is not enough to prevent the compiler from generating implementation for other types ? What is wrong in the below example: imagine I split up the template declaration and definition into two separate files:</p>
  
  <p>A.h</p>
  
  <pre><code>#pragma once
  
  template&lt;typename T&gt;
  class A
  {
  public:
      A(T t);
  
  private:
      T _t;
  };
  </code></pre>
  
  <p>A.cpp</p>
  
  <pre><code>#include "A.h"
  
  template&lt;typename T&gt;
  A&lt;T&gt;::A(T t) : _t(t) {}
  
  template class A&lt;int&gt;; // explicit instantiation
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "A.h"
  
  int main()
  {
      A&lt;int&gt; a(5); // fine, compiler generates header file,
                   // linker links with implementation from A.cpp file
  
      // A&lt;float&gt; b(3.14f); // linker error, as expected
  }
  </code></pre>
  
  <p>Is there any compilation time overhead in example above? If I understand correctly, in such case, using explicit instantiation <strong>definition</strong> in a separate *.cpp file (along with template's implementation) I make the compiler to be unable to implicitly instantiate the template with any other types. As such, why there is a separate syntax for explicit instantiation declaration ? </p>
  
  <p>How can explicit instantiation <strong>declaration</strong> speed up compilation time, if I had ALREADY hidden the implementation in A.cpp file using explicit instantiation <strong>definition</strong>, and prevented compiler from generating body for other types. Is the "explicit instantiation declaration" somehow related to "explicit instantiation definition", I mean should I use them both, or these are completely separate features (e.g. explicit instantiation declaration can be used only if explicit instantiation definition has not been used) ?</p>
  
  <p>Am I thinking right, that the explicit instantiation definition is only to trigger error in case no other translation unit instantiates the template using given type?</p>