title: (Edit) How to dll export a template specialization in Windows, with the source declared in a cpp file
body: <p><strong>Edit</strong> This question has been heavily modified</p>
  
  <p>I am trying to export a template specialization from a dll that is defined in a cpp file (purely for the purpose of exporting the template).</p>
  
  <p>The first attempt ("First Attempt") at this based on using dllimport/export in the header turned out to work both in compilation and at runtime however it produced a C4661 warning (no suitable definition provided for explicit template instantiation request). This is what I first asked the question on. However @AnT answer showed my mistake and I attempted to correct this with the "Second Attempt" this however also results in a warning as well.</p>
  
  <p><strong>First Attempt</strong></p>
  
  <p>foo.h</p>
  
  <pre><code>#ifndef _foo_h_ //header guard
  #define _foo_h_
  
  #ifdef EXAMPLE_FOO_EXPORTS
  #define EXAMPLE_FOO_EXPORT __declspec(dllexport)
  #define EXAMPLE_FOO_EXTERN
  #else
  #define EXAMPLE_FOO_EXPORT __declspec(dllimport)
  #define EXAMPLE_FOO_EXTERN extern
  #endif
  
  template&lt;typename _Type&gt;
  class foo
  {
  public:
      _Type value();
  };
  
  EXAMPLE_FOO_EXTERN template class EXAMPLE_FOO_EXPORT foo&lt;int&gt;;
  
  #endif//_foo_h_
  </code></pre>
  
  <p>foo.cpp</p>
  
  <pre><code>#include "foo.h"
  
  template&lt;typename _Type&gt; 
  _Type foo&lt;_Type&gt;::value()
  {
      return (_Type)1;
  }
  </code></pre>
  
  <p>I have a piece of test code in the dll to output a value.</p>
  
  <p>test.cpp</p>
  
  <pre><code>#include "foo.h"
  
  int readValue()
  {
      foo&lt;int&gt; test;
  
      return test.value();
  }
  </code></pre>
  
  <p><strong>Second Attempt</strong></p>
  
  <p>Going off of @AnT's note of the standard, instantiating in the cpp works but I am left with another issue (granted this is all VC-fu). Changing the code to below to make sure instantiation is done in the cpp (per standard and to get dll export working), I get a warning C4251: 'bar::value': class 'foo' needs to have dll-interface to be used by clients of struct 'bar'.</p>
  
  <p>foo.h</p>
  
  <pre><code>#ifndef _foo_h_ //header guard
  #define _foo_h_
  
  #ifdef EXAMPLE_FOO_EXPORTS
  #define EXAMPLE_FOO_EXPORT __declspec(dllexport)
  #define EXAMPLE_FOO_EXTERN
  #else
  #define EXAMPLE_FOO_EXPORT __declspec(dllimport)
  #define EXAMPLE_FOO_EXTERN extern
  #endif
  
  template&lt;typename _Type&gt;
  class foo
  {
  public:
      _Type value();
  };
  
  typedef foo&lt;int&gt; fooInt;
  
  #ifndef EXAMPLE_FOO_EXPORTS
  extern template class __declspec(dllimport) foo&lt;int&gt;;
  #endif
  
  struct EXAMPLE_FOO_EXPORT bar
  {
      fooInt value;
  };
  #endif//_foo_h_
  </code></pre>
  
  <p>foo.cpp</p>
  
  <pre><code>#include "foo.h"
  
  template&lt;typename _Type&gt; 
  _Type foo&lt;_Type&gt;::value()
  {
      return (_Type)1;
  }
  
  #ifdef EXAMPLE_FOO_EXPORTS
  template class __declspec(dllexport) foo&lt;int&gt;;
  #endif
  </code></pre>
  
  <p>I believe the bar struct is instantiating the foo template before it is instantiated in the cpp with the __declspec(dllexport). Besides the fact that "this is crazy, this is all wrong and why are you doing this", is there a way around this or is this just a peculiarity of windows dll import/export and it is what it is?</p>