title: Template specialization - different behaviour between MSVC and GCC/MinGW
body: <p>I specialized a template and encountered a different beahviour between the MSVC compiler and MinGW/GCC. Here is the header file:</p>
  
  <pre><code>// MyClass.h
  #ifndef MYCLASS_HEADER
  #define MYCLASS_HEADER
  
  template&lt;typename T&gt;
  class MyClass {
  public:
     virtual void doSomething() {
        // some code
     }
  };
  
  // specialization prototype here
  
  #endif
  </code></pre>
  
  <p>Now, the difference. To avoid multiple definitions, i specialized only a prototype in the header file, the implementation is in a cpp file. The specialization is the problem. the GCC/MinGW compiler accepts only this one:</p>
  
  <pre><code>template&lt;&gt; void MyClass&lt;int&gt;::doSomething(); // GCC version
  </code></pre>
  
  <p>And MSVC only this:</p>
  
  <pre><code>extern template void MyClass&lt;int&gt;::doSomething(); // MSVC version
  </code></pre>
  
  <p>The implementation in the CPP file is the same for both:</p>
  
  <pre><code>// MyClass.cpp
  #include "MyClass.h"
  
  template&lt;&gt; void MyClass&lt;int&gt;::doSomething() {
     // some code
  }
  </code></pre>
  
  <p>with the GCC prototype the MSVC compiler throws an "unresolved external symbol" error, GCC with the MSVC version throws "specialization of ... after instantiation".</p>
  
  <p>At the moment, i have the workaround:</p>
  
  <pre><code>#ifdef _MSC_VER
  extern template
  #else
  template&lt;&gt;
  #endif
  void MyClass&lt;int&gt;::doSomething();
  </code></pre>
  
  <p>in the header file. It works, but i don't like it. Is there a way to avoid the compiler specific switch?</p>