0

I'm having trouble getting a CRTP mixin to work.

Here is the stripped down implementation:

template < typename T >
class AutoSList : public T {
public:
  AutoSList() {}

  ~AutoSList() : _next(nullptr) {}


private:
  static T* _head;
  static T* _tail;
  T* _next;

};

// I really hate this syntax.
template <typename T>
T*  AutoSList<T>::_head = nullptr;
template <typename T>
T*  AutoSList<T>::_tail = nullptr;

class itsybase : public AutoSList < itsybase >
{

};

I'm using VS2013 and getting the following errors:

   error C2504: 'itsybase' : base class undefined
 : see reference to class template instantiation 'AutoSList<itsybase>' being compiled

I have no idea what's going wrong, any suggestions?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 1
    It's a typo. Your class name is Auto**S**List but you declare a constructor by the name of AutoList. Same with destructor. – eerorika Feb 25 '15 at 14:12
  • @AllanDeutsch: please do not change the code or other parts of the question after answers have been posted. that can easily invalidate the answers. instead, if you must, *add* to the question, but if that information makes it very different, then ask a *new* question. – Cheers and hth. - Alf Feb 25 '15 at 15:03

2 Answers2

5

There's 2 problems causing those compilation errors. First is a typo causing mismatch with c-tor/d-tor and class name.

The second problem is that you're trying to inherit T in the parent template. That's not possible in CRTP because the type will not be complete by the time the template is instantiated. That would cause an infinitely recursive inheritance anyway: itsybase inherits AutoSList<itsybase> which inherits itsybase which inherits AutoSList<itsybase>...

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    http://stackoverflow.com/a/4173298/1514515 I looked at this and other similar posts, it appears to be doing the same thing. EDIT: Nevermind I'm an idiot. – Allan Deutsch Feb 25 '15 at 14:31
2

It's a typo, as user2079303 suggested. Here's what clang++ says about it:

$ clang++ -std=c++11 -c go.cpp 
go.cpp:6:5: error: missing return type for function 'AutoList'; did you mean the constructor name 'AutoSList'?
    AutoList() {}
    ^~~~~~~~
    AutoSList
go.cpp:8:6: error: expected the class name after '~' to name a destructor
    ~AutoList() {}
     ^~~~~~~~
     AutoSList
go.cpp:20:19: error: no member named '_head' in 'AutoSList<T>'
T*  AutoSList<T>::_head = nullptr;
    ~~~~~~~~~~~~~~^
go.cpp:24:25: error: use of undeclared identifier 'ADLib'
class itsybase : public ADLib::AutoSList < itsybase >
                        ^
4 errors generated.
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • Thanks, I've updated it to reflect those changes, still got a couple errors. – Allan Deutsch Feb 25 '15 at 14:24
  • 1
    @AllanDeutsch please explicitly outline updates in your question, otherwise you're invalidating the answers. If the update prompts a completely new problem, please open a new question. – Quentin Feb 25 '15 at 14:27