title: CRTP, forward declarations and templates in cpp files
body: <p>I'm using the CRTP pattern to create an interface, which other classes will derive from.</p>
  
  <p>In the interface I forward declare a structure (important because I don't want to drag other stuff in the interface), but I include its definition in the cpp file which defines the interface.</p>
  
  <p><strong>Interface.h</strong></p>
  
  <pre><code>#ifndef INTERFACE_H_INCLUDED
  #define INTERFACE_H_INCLUDED
  
  // forward declaration
  class ForwardDecl;
  
  template &lt;class Derived&gt;
  class Interface
  {
  public:
      ForwardDecl interfaceMethod();
  
  };
  
  #endif // INTERFACE_H_INCLUDED
  </code></pre>
  
  <p><strong>ForwardDecl.h</strong></p>
  
  <pre><code>#ifndef FORWARDDECL_H_INCLUDED
  #define FORWARDDECL_H_INCLUDED
  
  struct ForwardDecl
  {
      ForwardDecl(int i):internal(i)
      {}
  
      int internal;
  };
  
  #endif // FORWARDDECL_H_INCLUDED
  </code></pre>
  
  <p><strong>Interface.cpp</strong></p>
  
  <pre><code>#include "Interface.h"
  #include "ForwardDecl.h"
  
  template&lt;class Derived&gt;
  ForwardDecl Interface&lt;Derived&gt;::interfaceMethod()
  {
      return static_cast&lt;Derived *&gt;(this)-&gt;implementation_func();
  }
  </code></pre>
  
  <p>And this is the implementation which implements the interface</p>
  
  <p><strong>Implementation.h</strong></p>
  
  <pre><code>#ifndef IMPLEMENTATION_H_INCLUDED
  #define IMPLEMENTATION_H_INCLUDED
  #include "Interface.h"
  class ForwardDecl;
  
  class Implementation: public Interface&lt;Implementation&gt;
  {
      friend class Interface&lt;Implementation&gt;;
  private:
      ForwardDecl implementation_func();
  
  };
  
  
  #endif // IMPLEMENTATION_H_INCLUDED
  </code></pre>
  
  <p><strong>Implementation.cpp</strong></p>
  
  <pre><code>#include "Implementation.h"
  #include "ForwardDecl.h"
  #include &lt;iostream&gt;
  struct ForwardDecl Implementation::implementation_func()
  {
      ForwardDecl fd(42);
      std::cout &lt;&lt; fd.internal &lt;&lt; std::endl;
  
      return fd;
  }
  </code></pre>
  
  <p>And the main file</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Implementation.h"
  #include "ForwardDecl.h"
  using namespace std;
  
  int main()
  {
      Implementation impl;
  
      ForwardDecl fd = impl.interfaceMethod();
      cout &lt;&lt; fd.internal &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>I get linking errors on both VS and GCC.</p>
  
  <p>Any workaround? Thank you.</p>