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?

πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
Nick
  • 8,953
  • 3
  • 39
  • 69

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,185
  • 7
  • 36
  • 82