title: C++ Unrolled loop at compile time
body: <p>I have several segments of code in my c++ project which look something like this:</p>
  <pre><code>system&lt;0&gt;::global_instance.do_something();
  system&lt;1&gt;::global_instance.do_something();
  system&lt;2&gt;::global_instance.do_something();
  system&lt;3&gt;::global_instance.do_something();
  //...
  system&lt;29&gt;::global_instance.do_something();
  system&lt;30&gt;::global_instance.do_something();
  system&lt;31&gt;::global_instance.do_something();
  </code></pre>
  <p>It seems a bit repetitive, right? It would be great if I could just do something like this:</p>
  <pre><code>for(int i = 0; i &lt; 32; i++)
  {
    system&lt;i&gt;::global_instance.do_something();
  }
  </code></pre>
  <p>Unfortunately, that won't work because the value of <code>i</code> isn't known at compile time and therefore cannot be used as a template parameter for <code>system</code>.</p>
  <p>What I need is a way to expand or unroll loops at compile time.</p>
  <p>I've seen some implementations which use templates to achieve unrolling, but they don't work for what I'm trying to do. Ideally, the loop unrolling would take place during preprocessing, and could take any arbitrary statement as input.</p>
  <p>For example, this:</p>
  <pre><code>#unroll NUM 0 5
  foo&lt;NUM&gt;();
  #endunroll
  </code></pre>
  <p>Would get translated into this:</p>
  <pre><code>foo&lt;0&gt;();
  foo&lt;1&gt;();
  foo&lt;2&gt;();
  foo&lt;3&gt;();
  foo&lt;4&gt;();
  foo&lt;5&gt;();
  </code></pre>
  <p>And this:</p>
  <pre><code>#unroll NUM 0 5
  blah blah blah NUM
  #endunroll
  </code></pre>
  <p>Would get translated into this:</p>
  <pre><code>blah blah blah 0
  blah blah blah 1
  blah blah blah 2
  blah blah blah 3
  blah blah blah 4
  blah blah blah 5
  </code></pre>
  <p>Even though <code>blah blah blah NUM</code> is a syntactically incorrect statement, it should still get copied and every instance of the token NUM should get replaced by the appropriate number value.</p>
  <p>Is there any way to achieve this in vanilla c++? Or are there any special c++ compilers which add this functionality?</p>
  <p>Edit: I found an answer from @HTNW in the comments.</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  template&lt;auto begin, auto end&gt;
  inline void unroll(auto f)
  {
    if constexpr(begin &lt; end)
    {
      f.template operator()&lt;begin&gt;();
      unroll&lt;begin + 1, end&gt;(f);
    }
  }
  
  template&lt;int NUM&gt;
  struct thingy
  {
    static void print(){cout &lt;&lt; NUM &lt;&lt; endl;}
  };
  
  int main()
  {
    unroll&lt;0,8&gt;([]&lt;int i&gt;()
    {
      thingy&lt;i&gt;::print();
    });
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>0
  1
  2
  3
  4
  5
  6
  7
  </code></pre>
  <p>The line which makes this work is <code>f.template operator()&lt;begin&gt;();</code>. I have no idea what this line means and I've never seen it before in my life. If someone could explain it to me, that would be great.</p>