实现 CRTP 链表混合C++
Implementing a CRTP linked list mix-in C++
我无法让CRTP混合工作。
以下是精简的实现:
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
我不知道出了什么问题,有什么建议吗?
有
2 个问题导致这些编译错误。首先是拼写错误导致与 c-tor/d-tor 和类名不匹配。
第二个问题是您尝试继承父模板中的T
。这在 CRTP 中是不可能的,因为在实例化模板时,类型将不完整。无论如何,这都会导致无限递归继承:itsybase
继承AutoSList<itsybase>
继承itsybase
继承AutoSList<itsybase>
......
正如
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.
相关文章:
- 混合组合和继承的C++问题
- 在混合代码库中将C转换为C++时出现许多包含错误
- D3D11-将混合权重和索引传递到顶点着色器
- C++分数混合比较运算符错误
- 是否可以混合使用SFINAE和模板专业化?
- 如何在 Python C++ 混合库中调试非确定性分段错误?
- 使用 Boost.Spirit 解析具有混合数据类型的 OBJ 文件?
- C++11 中的混合列表初始化
- C++以迭代方式搜索混合类型地图
- #pragma(*诊断)当将Clang分析器与GCC编译器混合时
- 混合 Rcpp 模块和 Rcpp::export
- C++/CLI 混合托管/本机 DLL 不起作用
- 在C++中混合覆盖和重载
- 关于 Direct3D9 中的 alpha 混合的问题
- 使用英特尔内部函数 (AVX) 中的混合说明
- C++ 中混合二进制/文本日志记录的最佳做法
- Fortran 和 C++ 的混合编程:Fortran 不能调用C++子程序
- 错误:在 C++ 目标 c 混合'self'使用未声明的标识符
- 是否有一个版本的glCopyImageSubData在Openg GL中使用混合?
- 为什么这个混合继承程序给出错误的输出?