title: Macro definition ARRAY_SIZE
body: <p>I encountered the following macro definition when reading the globals.h in the Google <a href="http://en.wikipedia.org/wiki/V8_%28JavaScript_engine%29" rel="nofollow noreferrer">V8</a> project.</p>
  
  <pre><code>// The expression ARRAY_SIZE(a) is a compile-time constant of type
  // size_t which represents the number of elements of the given
  // array. You should only use ARRAY_SIZE on statically allocated
  // arrays.
  
  #define ARRAY_SIZE(a)                               \
    ((sizeof(a) / sizeof(*(a))) /                     \
    static_cast&lt;size_t&gt;(!(sizeof(a) % sizeof(*(a)))))
  </code></pre>
  
  <p>My question is the latter part: <code>static_cast&lt;size_t&gt;(!(sizeof(a) % sizeof(*(a)))))</code>. One thing in my mind is the following: Since the latter part will always evaluates to <code>1</code>, which is of type <code>size_t</code>, the whole expression will be promoted to <code>size_t</code>.</p>
  
  <p>If this assumption is correct, then there comes another question: since the return type of <code>sizeof</code> operator is size_t, why is such a promotion necessary? What's the benefit of defining a macro in this way?</p>