title: C++ templated function and forward declarations
body: <p>I'm working on some code that compiles and links (and even has released commercial products) on Windows using MSVC. It doesn't compile with GCC though, I get the following errors:</p>
  
  <pre><code>.../CBaseValue.h: In member function 'bool CBaseValue::InstanceOf()':
  .../CBaseValue.h:90:18: error: invalid use of incomplete type 'struct CValueType'
  .../CBaseValue.h:11:7: error: forward declaration of 'struct CValueType'
  </code></pre>
  
  <h1> CBaseValue.h</h1>
  
  <pre><code>class CValueType;
  
  class CBaseValue {
  public:
  
  ...
  
      template &lt;typename _Type&gt;
      bool InstanceOf() {
          CValueType* pType = GetType();
          if(pType == NULL) {
              return false;
          }
          else {
              return pType-&gt;IsDerivedFrom&lt;_Type&gt;();
          }
      }
  
  ...
  
  }
  </code></pre>
  
  <h1> CValueType.h</h1>
  
  <pre><code>class CValueType : public CBaseValue  {
  public:
  
  ...
  
      template &lt;typename _Type&gt;
      bool IsDerivedFrom() {
          return IsDerivedFrom(_Type::TYPEDATA);
      }
  
  ...
  
  }
  </code></pre>
  
  <p>I understand why this is a problem. The base class (CBaseValue) has a templated function that uses a derived class (in this case CValueType).</p>
  
  <p>It looks like MSVC isn't exactly obeying the C++ spec here and I've just been bitten by it. But the MSVC behavior of using the forward declaration until code calling the templated function is actually compiled is also more desirable right now. Does anybody know of a work-around where I can get this code working with GCC without having to rewrite a lot of base code?</p>
  
  <p>From my own research it looks like passing '-fno-implicit-templates' to g++ would help but then I'd need to explicitly define the called template types. There are a lot of them so if I can avoid that I'd prefer it. If the general consensus is that this is my best option... so be it!</p>
  
  <p>And in case anybody is wondering, I'm porting the code over to the Mac which is why we're now using GCC.</p>