CRTP的替代方案
Alternatives to CRTP
假设我们有以下virtual method
类:
struct icountable{
virtual int count() = 0;
bool empty(){
return count() == 0;
}
}
struct list : public icountable {
...
}
现在假设这个可以用CRTP
重写。应该看起来或多或少像:
template <typename T>
struct icountable{
bool empty(){
return static_cast<T*>(this)->count() == 0;
}
}
struct list : public icountable<list> {
...
}
现在假设类本身不需要使用empty()方法。然后我们可以这样做:
template <typename T>
struct icountable : public T{
bool empty(){
return count() == 0;
}
}
struct list_base{
...
}
typedef icountable<list_base> list;
我的问题是第三个例子。这就是所谓的traits
吗?如果我使用这些有什么优点/缺点吗? 正如评论所说,这是混合的概念,您可以在这里找到有关它的信息。
特征是不同的,在这里你可以找到一个基本的例子。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- visual c++,如何获取解决方案目录中的代码
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- C++11 中不同类型的对象的 std::array 的替代方案
- CRTP 单一实例不完整类型或非文本类型
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 无法在问题解决方案中执行输出逻辑
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc
- 基于CRTP的解决方案会是什么样子
- CRTP的替代方案