More C++ Idioms/Empty Base Optimization

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
 template <class Base, class Member>
   struct BaseOpt : Base {
     Member m;
     BaseOpt(Base const& b, Member const& mem) 
       : Base(b), m(mem) { }
   };


Using this template, our list<> declaration appears as in Listing 6.

Listing 6: The Best Way to Eliminate Bloat

 template <class T, class Alloc = allocator<T> >
   class list {
     . . .
     struct Node { . . . };
     BaseOpt<Alloc,Node*> head_;
     
    public:
     explicit list(Alloc const& a = Alloc())
       : head_(a,0) { . . . }
     . . .
   };


read here http://www.cantrip.org/emptyopt.html