0

I have rather simple, I imagine, question about CRTP, 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 ;).

Here is the code (it's sort of an attempt to do something similar to STL containers):

template< typename tT >
struct TBase
{
  typedef tT T;
};

template< typename tTBase >
struct TTraitsBase
{
  typedef typename tTBase::T T;
};

template< typename tTHelpee, typename tTTraits >
struct THelper
{

  typedef typename tTTraits::T T;
  typedef typename tTHelpee::T Th; /* This generates a compiler error:
                                      'T' not being a member of TDerived<tT> */
  T Foo( void )
  {
   return static_cast< tTHelpee* > ( this )->mVal;
  }
};

template< typename tT >
struct TDerived : TBase< tT > , THelper< TDerived< tT > , TTraitsBase< TBase< tT > > >
{
 using TBase< tT >::T;
  T mVal;
};

int main()
{
 TDerived< int >::T lTmp = -1;
 TDerived< int > lObj;

 lObj.mVal = -1;
 std::cout << lObj.Foo() << std::endl;

 return 0;
}

Everything compiles if I comment the offending typedef typename _THelpee::T Th;. And that what confuses me: if compiler does not like typedef typename _THelpee::T Th;, why does it let through static_cast< _THelpee* > ( this )->mVal? I assume, it has something to do with not being able to instantiate THelper when instantiating TDerived, but no clear understanding. Can someone, please, give a brief explanation and/or some references on what is happening here? Thank you.

EDIT: removed '_T' prefix.

lapk
  • 3,838
  • 1
  • 23
  • 28
  • 2
    Identifiers starting with an [_ and followed by a capital letter are reserved](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797) for the implementation. – Flexo Aug 29 '11 at 11:54
  • Do not use underscore-capital for your own identifiers, that's forbidden. – Kerrek SB Aug 29 '11 at 11:54

3 Answers3

2

There are couple of problem with the code posted. Since, I couldn't make out the intention, I will list what I found erroneous.

(1) Using an incomplete type:

template< typename _T >
struct TDerived : TBase< _T > , THelper< TDerived< _T > , TTraitsBase< TBase< _T > > >
                                         ^^^^^^^^^^^^^^ it's incomplete

IMO when you are defining the body of TDerived<_T>, you should not use the same as a parameter to anything.

(2) Improper type definition.

using TBase< _T >::T;

should be,

typedef typename TBase< _T >::T T;
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    It's okay for a template parameter to be an incomplete type (and that's always the case with CRTP). But as with all incomplete types, there are restrictions on valid uses of that type. – aschepler Aug 29 '11 at 12:13
  • Thanks for the input, iammilind. I think, aschepler answered it. I am reluctant to break the rules and enter a discussion, since I am new here. – lapk Aug 29 '11 at 12:20
2

Implicitly instantiating a class does not instantiate its member function definitions. Each member of a class template is instantiated only when used (unless you use explicit instantiation).

The first thing you implicitly instantiate is TDerived<int>, on the first line of main. To instantiate that, the compiler looks up the TDerived template, sees that there are a couple of dependent base classes, so tries to instantiate those. TBase<int> instantiates with no problem. But on trying to instantiate THelper< TDerived<int>, TTraitsBase< TBase<int> > >, there's an attempt to get a member TDerived<int>::T, but TDerived<int> is still an incomplete type.

Instantiating THelper< ... > also notes that there is a member function int Foo(), but does not evaluate the semantics of its definition.

By the time you call lObj.Foo() from main, which instantiates that int THelper< ... >::Foo(), TDerived<int> is a complete type, so there's no problem there.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • Thanks! That explains it clearly. I obviously did not appreciate the function instantiating sequence. I did realize that `TDerived` was indeed incomplete, but could figure out why `Foo()` didn't cause any problems in this case. Now it's clear. – lapk Aug 29 '11 at 12:24
  • On a side note, is it considered normal to use `TTraits` or possible `TBase` template parameter to `THelper` to get access to those `typedefs` in `TDerived`? Or is there more "standard" way of doing that (reference on the subject would be greatly appreciated)? And thanks again for your earlier explanations. – lapk Aug 29 '11 at 12:33
1

I'd say you have a circular dependency in your CRTP (as one would have, by its very nature), but that means that the base template class isn't allowed to use any of its parameter class, because that very parameter class isn't defined yet - it's still an incomplete type at the time where Base<T> is instantiated.

So, this is OK:

template <typename T> struct Base
{
  T * impl;  // incomplete type is OK
};

class Foo : public Base<Foo> { /* ... */ };

But this isn't:

template <typename T> struct Base
{
  T x; // need to know T, but T depends on Base<T>!
};
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Thanks. I suppose you are saying the same thing as aschepler in his answer, with less detail. – lapk Aug 29 '11 at 12:40