title: Why do C++ classes without member variables occupy space?
body: <p>I found that both MSVC and GCC compilers allocate at least one byte per each class instance even if the class is a predicate with no member variables (or with just static member variables). The following code illustrates the point.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class A
  {
  public:
     bool operator()(int x) const
     {
        return x&gt;0;
     }
  };
  
  class B
  {
  public:
     static int v;
     static bool check(int x)
     {
        return x&gt;0;
     }
  };
  
  int B::v = 0;
  
  void test()
  {
     A a;
     B b;
     std::cout &lt;&lt; "sizeof(A)=" &lt;&lt; sizeof(A) &lt;&lt; "\n"
               &lt;&lt; "sizeof(a)=" &lt;&lt; sizeof(a) &lt;&lt; "\n"
               &lt;&lt; "sizeof(B)=" &lt;&lt; sizeof(B) &lt;&lt; "\n"
               &lt;&lt; "sizeof(b)=" &lt;&lt; sizeof(b) &lt;&lt; "\n";
  }
  
  int main()
  {
     test();
     return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>sizeof(A)=1
  sizeof(a)=1
  sizeof(B)=1
  sizeof(b)=1
  </code></pre>
  
  <p>My question is why does compiler need it? The only reason that I can come up with is ensure that all member var pointers differ so we can distinguish between two members of type A or B by comparing pointers to them. But the cost of this is quite severe when dealing with small-size containers. Considering possible data alignment, we can get up to 16 bytes per class without vars (?!). Suppose we have a custom container that will typically hold a few int values. Then consider an array of such containers (with about 1000000 members). The overhead will be 16*1000000! A typical case where it can happen is a container class with a comparison predicate stored in a member variable. Also, considering that a class instance should always occupy some space, what type of overhead should be expected when calling A()(value) ?</p>