title: C++ CRTP and accessing derived's nested typedefs from base
body: <p><strong>edit:</strong> I'll put a github link here when I am done altering my design for anyone who is interested.</p>
  
  <p><strong>Background</strong></p>
  
  <p>I'm replacing a <code>boost::intrusive</code>, <code>intrusive_set</code>, with my own implementation as 64-bit compiled intrusive-set stuffs 3 x 8-byte pointers into my container nodes. my container has a limit of 2^16 nodes so I can bring it down to 4-bytes per node with 2x 16-bit offset ordinals (which is a 6x reduction of size). </p>
  
  <p>In the example below <code>base</code> is the intrusive-set container. The <code>derived</code> class has a <code>std::vector&lt;container_entry_type&lt;entry_type&gt; &gt;</code>. obviously with this level of indirection I need to have a bunch of nested typedef's in derived, which I'd like to refer to in base.</p>
  
  <p>p.s., the containers are for the AST of data description language. The contained elements are therefore small data types and 3 x 8-bytes is very significant. Especially so since the containers are used to validate data-sets in tight loops.</p>
  
  <p><strong>The problem isolated</strong></p>
  
  <p>I want to achieve the following semantics:</p>
  
  <pre><code>template&lt;typename TWO&gt;
  class base
  {
  public:
    void foo(typename TWO::dummy &amp; d);
  };
  
  template&lt;typename DUMMY&gt;
  class derived
    : private base&lt; derived&lt;DUMMY&gt; &gt;
  {
  public:
    typedef DUMMY dummy;
  };
  
  struct tag{};
  
  int main()
  {
    derived&lt;tag&gt; foo;
  }
  </code></pre>
  
  <p>But I can't access the nested typedef from the base. This is what clang has to say about the matter:</p>
  
  <pre><code>main.cc: In instantiation of ‘base&lt;derived&lt;tag&gt; &gt;’:
  main.cc:9:7:   instantiated from ‘derived&lt;tag&gt;’
  main.cc:20:16:   instantiated from here
  main.cc:5:8: error: no type named ‘dummy’ in ‘class derived&lt;tag&gt;’
  </code></pre>
  
  <p>Instead I am having to do:</p>
  
  <pre><code>template&lt;typename type_key&gt;
  class traits
  {
  public:
    typedef type_key dummy;
  };
  
  template&lt;typename TWO, typename type_key&gt;
  class base
  { 
  public:
    void foo(typename traits&lt;type_key&gt;::dummy &amp; d);
  };
  
  template&lt;typename DUMMY&gt;
  class derived
    : private base&lt; derived&lt;DUMMY&gt;, DUMMY &gt;
  {
  public:
    typedef DUMMY dummy;
  };
  
  struct tag{};
  
  int main()
  {
    derived&lt;tag&gt; foo;
  }
  </code></pre>
  
  <p>Is this the only way to achieve my use-case ? it just makes things a whole lot more verbose. I suppose derived could also derive from traits to save some keystrokes.</p>
  
  <p>Another choice is to not use derivation and to wire the logic straight into what is currently derived. However, I'd like to individually unit test base. </p>