实现CRTP链表混合C++

All*_*sch 0 c++ templates mixins crtp

我无法让CRTP mixin工作.

这是剥离的实现:

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 >
{

};
Run Code Online (Sandbox Code Playgroud)

我正在使用VS2013并收到以下错误:

   error C2504: 'itsybase' : base class undefined
 : see reference to class template instantiation 'AutoSList<itsybase>' being compiled
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题,有什么建议吗?

eer*_*ika 5

导致这些编译错误的问题有两个.首先是输入错误与c-tor/d-tor和类名不匹配.

第二个问题是您尝试T在父模板中继承.这在CRTP中是不可能的,因为在实例化模板时类型不会完整.这将导致无限递归继承:itsybase继承AutoSList<itsybase>哪些继承itsybase继承AutoSList<itsybase>...