_The Empty Member C++ Optimization_
by Nathan Myers


Listing One
template <class T>
class allocator {   // an empty class
  static T* allocate(size_t n)
    { return (T*) ::operator new(n * sizeof T); }
  . . .
};

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


Listing Three 
struct Bar { };
struct Foo {
  struct Bar a[2];
  struct Bar b;
};
Foo f;

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

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

Listing Six
template <class Base, class Member>
struct BaseOpt : Base {
  Member m;
  BaseOpt(Base const& b, Member const& mem) 
   : Base(b), m(mem) { }
};

Listing Seven
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) { . . . }
  . . .
};

