简体   繁体   English

实施CRTP链表混入C ++

[英]Implementing a CRTP linked list mix-in C++

I'm having trouble getting a CRTP mixin to work. 我无法让CRTP mixin正常工作。

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: 我正在使用VS2013并收到以下错误:

   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? 我不知道出了什么问题,有什么建议吗?

There's 2 problems causing those compilation errors. 导致这些编译错误的有2个问题。 First is a typo causing mismatch with c-tor/d-tor and class name. 首先是一个错字,导致与c-tor / d-tor和类名不匹配。

The second problem is that you're trying to inherit T in the parent template. 第二个问题是您要在父模板中继承T That's not possible in CRTP because the type will not be complete by the time the template is instantiated. 这在CRTP中是不可能的,因为在实例化模板时类型将不完整。 That would cause an infinitely recursive inheritance anyway: itsybase inherits AutoSList<itsybase> which inherits itsybase which inherits AutoSList<itsybase> ... 无论如何,这将导致无限递归继承: itsybase继承了AutoSList<itsybase> ,后者继承了itsybase ,后者继承了AutoSList<itsybase>

It's a typo, as user2079303 suggested. 正如user2079303建议的,这是一个错字。 Here's what clang++ says about it: 这是clang++所说的:

$ 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM