假设我们有下面的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> {
...
}现在假设类本身不需要使用空()方法。然后我们可以做这样的事情:
template <typename T>
struct icountable : public T{
bool empty(){
return count() == 0;
}
}
struct list_base{
...
}
typedef icountable<list_base> list;我的问题是第三个例子。这就是所谓的traits吗?如果我使用这些,有什么好处/缺点吗?
发布于 2015-10-06 14:12:14
正如评论所述,这是一个混合概念,您可以找到有关它的信息这里。
这些特征是不同的,这里你可以找到一个基本的例子。
https://stackoverflow.com/questions/32971641
复制相似问题