Note that there are some explanatory texts on larger screens.

plurals
  1. POCRTP-related compiler error on typedef
    text
    copied!<p>I have rather simple, I imagine, question about <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="nofollow">CRTP</a>, but I cannot seem to find an answer to it. Probably, because it is so simple, no one thought of asking it. I am new to the concept, so, please don't laugh too hard ;).</p> <p> Here is the code (it's sort of an attempt to do something similar to STL containers):</p> <pre class="lang-c++ prettyprint-override"><code>template&lt; typename tT &gt; struct TBase { typedef tT T; }; template&lt; typename tTBase &gt; struct TTraitsBase { typedef typename tTBase::T T; }; template&lt; typename tTHelpee, typename tTTraits &gt; struct THelper { typedef typename tTTraits::T T; typedef typename tTHelpee::T Th; /* This generates a compiler error: 'T' not being a member of TDerived&lt;tT&gt; */ T Foo( void ) { return static_cast&lt; tTHelpee* &gt; ( this )-&gt;mVal; } }; template&lt; typename tT &gt; struct TDerived : TBase&lt; tT &gt; , THelper&lt; TDerived&lt; tT &gt; , TTraitsBase&lt; TBase&lt; tT &gt; &gt; &gt; { using TBase&lt; tT &gt;::T; T mVal; }; int main() { TDerived&lt; int &gt;::T lTmp = -1; TDerived&lt; int &gt; lObj; lObj.mVal = -1; std::cout &lt;&lt; lObj.Foo() &lt;&lt; std::endl; return 0; } </code></pre> <p>Everything compiles if I comment the offending <code>typedef typename _THelpee::T Th;</code>. And that what confuses me: if compiler does not like <code>typedef typename _THelpee::T Th;</code>, why does it let through <code>static_cast&lt; _THelpee* &gt; ( this )-&gt;mVal</code>? I assume, it has something to do with not being able to instantiate <code>THelper</code> when instantiating <code>TDerived</code>, but no clear understanding. Can someone, please, give a brief explanation and/or some references on what is happening here? Thank you.</p> <p>EDIT: removed '_T' prefix.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload