title: How to separate definition from the declaration for a class template using 'extern' in C++11 in a library (dll, so,..)
body: <p>I am a developer of an Open Source library. One of our classes is templated for a bunch of different types. Currently the definition resides in the header file, which has a negative effect on compilation times and also forces users to include more headers than needed. My goal is the following:</p>
  
  <ul>
  <li><p>To reduce compilation time, I want to make use of the the explicit instantiation declaration introduced with C++11.</p></li>
  <li><p>The definition of the class methods and members, which all are static, must be separate from the declaration in an implementation file. They should be available for usage outside and inside the library without users having to do an explicit instantiation definition or anything like this.</p></li>
  <li><p>This must run cross-platform on all common compilers that support C++11 (Visual Studio 2013+, GCC, etc)</p></li>
  </ul>
  
  <p>C++11 is providing new features for class templates, in specific the "<a href="http://en.cppreference.com/w/cpp/language/class_template" rel="noreferrer">explicit instantiation declaration</a>". As far as I understand this can be used in this context. Previous questions dealt with this in similar contexts, e.g. <a href="https://stackoverflow.com/questions/6870885/how-to-use-extern-template">How to use extern template</a> and <a href="https://stackoverflow.com/questions/22575883/separating-definition-instantiation-of-template-classes-without-extern">Separating definition/instantiation of template classes without &#39;extern&#39;</a> but those do not deal with library exports and their solution cause linker errors if a client tries to use the shared library.</p>
  
  <p>Currently I have managed to implement this in a way it compiles, links and runs on Visual Studio 2015, but I am unsure if I use the keywords correctly, especially the __declspec one in this case. This is what I got (simplified):</p>
  
  <pre><code>// class.h
  template&lt;typename T&gt;
  class PropertyHelper;
  
  template&lt;typename T&gt;
  class PropertyHelper&lt;const T&gt;
  {
  public:
      typedef typename PropertyHelper&lt;T&gt;::return_type return_type;
  
      static inline return_type fromString(const String&amp; str)
      {
          return PropertyHelper&lt;T&gt;::fromString(str);
      }
  
      static const int SomeValue;
  };
  
  
  template&lt;&gt;
  class EXPORTDEF PropertyHelper&lt;float&gt;
  {
  public:
      typedef float return_type;
  
      static return_type fromString(const String&amp; str);
  
      static const int SomeValue;
  };
  
  extern template EXPORTDEF class PropertyHelper&lt;float&gt;;
  </code></pre>
  
  <p>The last line is the explicit instantiation declaration. As far as I understand this means that clients do not have to declare this themselves every time. <code>EXPORTDEF</code> is a define that is either __declspec(dllexport) or __declspec(dllimport) on Windows. I am not sure if I need to place this in the line above, because the following also compiles, links and runs:</p>
  
  <pre><code>extern template class PropertyHelper&lt;float&gt;;
  </code></pre>
  
  <p>The cpp file looks like this:</p>
  
  <pre><code>const int PropertyHelper&lt;float&gt;::SomeValue(12);
  
  PropertyHelper&lt;float&gt;::return_type
  PropertyHelper&lt;float&gt;::fromString(const String&amp; str)
  {
      float val = 0.0f;
  
      if (str.empty())
          return val;
  
      //Some code here...
  
      return val;
  }
  
  template class PropertyHelper&lt;float&gt;;
  </code></pre>
  
  <p>The last line is the explicit instantiation definition.</p>
  
  <p>So my question is most of all if I did everything correctly here according to the C++11 standard and secondly (if first is true) if the __declspec keyword is redundant in the context of the explicit instantiation declaration, or what I should do about it, as I did not find proper information in the MSDN docs.</p>