2

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Nick
  • 9,962
  • 4
  • 42
  • 80

1 Answers1

0

As the comments say, this is the mix-in concept, and you can find informations about it here.

The traits are different and here you can find a basic example.

Community
  • 1
  • 1
sop
  • 3,445
  • 8
  • 41
  • 84