title: clang: Force loop unroll for specific loop
body: <p>Is there a way to tell clang to unroll a specific loop?</p>
  
  <hr>
  
  <p>Googling for an answer gives me command-line options which will affect the whole compilant and not a single loop.</p>
  
  <hr>
  
  <p>There is a similar question for GCC --- <a href="https://stackoverflow.com/questions/4071690/tell-gcc-to-specifically-unroll-a-loop">Tell gcc to specifically unroll a loop</a> --- but the answer provided there does not work with clang.</p>
  
  <p>Option 1 suggested there:</p>
  
  <pre><code>#pragma GCC optimize ("unroll-loops")
  </code></pre>
  
  <p>seems to be silently ignored. In fact</p>
  
  <pre><code>#pragma GCC akjhdfkjahsdkjfhskdfhd
  </code></pre>
  
  <p>is also silently ignored.</p>
  
  <p>Option 2:</p>
  
  <pre><code>__attribute__((optimize("unroll-loops")))
  </code></pre>
  
  <p>results in a warning:</p>
  
  <pre><code>warning: unknown attribute 'optimize' ignored [-Wattributes]
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>joshuanapoli provides a nice solution how to iterate via template metaprogramming and C++11 without creating a loop. The construct will be resolved at compile-time resulting in a repeatedly inlined body. While it is not exactly an answer to the question, it essentially achieves the same thing.</p>
  
  <p>That is why I am accepting the answer. However, if you happen to know how to use a standard C loop (<code>for</code>, <code>while</code>) and force an unroll it - please share the knowledge with us!</p>