我无法让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 >
{
};
我使用VS2013并收到以下错误:
error C2504: 'itsybase' : base class undefined
: see reference to class template instantiation 'AutoSList<itsybase>' being compiled
我不知道出了什么问题,有什么建议吗?
答案 0 :(得分:5)
导致这些编译错误的2个问题。首先是输入错误导致与c-tor / d-tor和类名不匹配。
第二个问题是您尝试在父模板中继承T
。这在CRTP中是不可能的,因为在实例化模板时类型不会完整。这将导致无限递归继承:itsybase
继承AutoSList<itsybase>
继承itsybase
继承AutoSList<itsybase>
...
答案 1 :(得分:2)
这是一个错字,正如user2079303
所建议的那样。以下是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.