title: Why doesn't this C++ template code compile?
body: <p>Does anyone know why this will not compile?  I've tried both VS 2008 and GCC 4.something and both spit out errors.  It doesn't matter whether or not I'm referencing "ThisFunctionDoesNotCompile()".</p>
  
  <p>I can workaround this by just passing 'InternalType' as a second template parameter to Base, but I'm still curious why this comes up as an error.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class DataClass
  {
  public:
      int m_data;
  };
  
  template&lt;typename DerivedType&gt;
  class Base
  {
  public:
      int ThisFunctionCompiles()
      {
          // No problems here.
  
          typename DerivedType::InternalType temp;
          temp.m_data = 5;
          return temp.m_data;
      }
  
      // error C2039: 'InternalType' : is not a member of 'Derived&lt;InInternalType&gt;'
      typename DerivedType::InternalType ThisFunctionDoesNotCompile()
      {
          return static_cast&lt;DerivedType*&gt;(this)-&gt;GetInternalData();
      }
  };
  
  template&lt;typename InInternalType&gt;
  class Derived : public Base&lt;Derived&lt;InInternalType&gt; &gt;
  {
  public:
      typedef InInternalType InternalType;
  
      InternalType GetInternalData()
      {
          return m_internalData;
      }
  
  private:
      InternalType m_internalData;
  
  
  public:
      void SetInternalData( int newVal )
      {
          m_internalData.m_data = newVal;
      }
  };
  
  int main()
  {
  
      Derived&lt;DataClass&gt; testDerived;
      testDerived.SetInternalData( 3 );
  
      cout &lt;&lt; testDerived.GetInternalData().m_data &lt;&lt; endl;
      cout &lt;&lt; testDerived.ThisFunctionCompiles() &lt;&lt; endl;
  
      // The compiler gives an error regardless of whether or not this is commented out.
      //cout &lt;&lt; testDerived.ThisFunctionDoesNotCompile().m_data &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>These are the errors I get in VS 2008:</p>
  
  <pre><code>1&gt;e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C2039: 'InternalType' : is not a member of 'Derived&lt;InInternalType&gt;'
  1&gt;        with
  1&gt;        [
  1&gt;            InInternalType=DataClass
  1&gt;        ]
  1&gt;        e:\test\generaltestprogram\generaltestprogram\main.cpp(35) : see reference to class template instantiation 'Base&lt;DerivedType&gt;' being compiled
  1&gt;        with
  1&gt;        [
  1&gt;            DerivedType=Derived&lt;DataClass&gt;
  1&gt;        ]
  1&gt;        e:\test\generaltestprogram\generaltestprogram\main.cpp(58) : see reference to class template instantiation 'Derived&lt;InInternalType&gt;' being compiled
  1&gt;        with
  1&gt;        [
  1&gt;            InInternalType=DataClass
  1&gt;        ]
  1&gt;e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C2146: syntax error : missing ';' before identifier 'ThisFunctionDoesNotCompile'
  1&gt;e:\test\generaltestprogram\generaltestprogram\main.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;e:\test\generaltestprogram\generaltestprogram\main.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1&gt;e:\test\generaltestprogram\generaltestprogram\main.cpp(28) : warning C4183: 'ThisFunctionDoesNotCompile': missing return type; assumed to be a member function returning 'int'
  </code></pre>
  
  <p>And these are what GCC gives me:</p>
  
  <pre><code>main.cpp: In instantiation of 'Base&lt;Derived&lt;DataClass&gt; &gt;':
  main.cpp:96:   instantiated from 'Derived&lt;DataClass&gt;'
  main.cpp:119:   instantiated from here
  main.cpp:88: error: no type named 'InternalType' in 'class Derived&lt;DataClass&gt;'
  </code></pre>