title: Why prefer template-based static assert over typedef-based static assert?
body: <p>There're two <a href="https://stackoverflow.com/questions/1980012/boost-static-assert-without-boost/1980056#1980056">widely used</a> implementations of static assert for versions of C++ that don't have built-in <code>static_assert</code>.</p>
  
  <p>The first one is used in Boost and uses <a href="https://stackoverflow.com/q/3099445">a template and a specialization of that template</a>:</p>
  
  <pre><code>template &lt;bool&gt; struct static_assert;
  template &lt;&gt; struct static_assert&lt;true&gt; {}; // only true is defined
  #define  STATIC_ASSERT(x) static_assert&lt;(x)&gt;()
  </code></pre>
  
  <p>Here once a condition to check is false the compiler is unable to find a generic version of template and compilation fails.</p>
  
  <p>the second uses a <code>typedef</code>:</p>
  
  <pre><code>#define STATIC_ASSERT( x ) typedef char __STATIC_ASSERT__[( x )?1:-1]
  </code></pre>
  
  <p>Here once a condition to check is violated the compiler attempts to <code>typedef</code> an array of size -1 and that's illegal hence a compile-time error.</p>
  
  <p>To me the latter is better since it is guaranteed to emit no code and also it can be used like this (from <a href="https://stackoverflow.com/q/6939103/57428">here</a>):</p>
  
  <pre><code>template&lt;int Shift&gt; class BinaryFlag {
      STATIC_ASSERT( 0 &lt;= Shift &amp;&amp; Shift &lt; sizeof( DWORD) * CHAR_BIT );
      public:
      static const DWORD FlagValue = static_cast&lt;DWORD&gt;( 1 &lt;&lt; Shift );
  };
  #define BINARY_FLAG( n ) CBinaryFlag&lt;n&gt;::FlagValue
  </code></pre>
  
  <p>while the former can't be used like that.</p>
  
  <p>Is there any reason to prefer the former implementation of static assert over the latter one?    </p>