Suppose we have following class with virtual method
:
struct icountable{
virtual int count() = 0;
bool empty(){
return count() == 0;
}
}
struct list : public icountable {
...
}
Now suppose this can be rewritten with CRTP
. Should look more or less like:
template <typename T>
struct icountable{
bool empty(){
return static_cast<T*>(this)->count() == 0;
}
}
struct list : public icountable<list> {
...
}
Now suppose the class itself does not need to use empty() method. Then we can do something like this:
template <typename T>
struct icountable : public T{
bool empty(){
return count() == 0;
}
}
struct list_base{
...
}
typedef icountable<list_base> list;
My question is for third example. Is this what is called traits
? Is there advantages / disadvantages if I use those?