title: Modern C++ way to repeat code for set number of times
body: <p>Very simply, is there a simpler way to repeat a block for a certain number of times, where the block inside does not need the counter variable? The trivial solution is of course</p>
  
  <pre><code>for (int i = 0; i &lt; repetitions; ++i) {
    //do your thing, i is not used here
  }
  </code></pre>
  
  <p>However, now that we have ranged for, standard algorithms and other fancy constructs for iterating over containers, in comparison this is actually starting to feel like a lot of boilerplate and details for what should be an even simpler case. For example we're not interested in the variable <code>i</code> at all etc.</p>
  
  <p>The closest thing to a concrete problem is this: when I encounter a for loop such as above, I need to scan through the code block to see if <code>i</code> is actually used, or if it's just a dummy counter. The declaration of a for loop which actually wants to do something with the integers 0 to <code>repetitions - 1</code> will look identical. So a <code>repeat (n)</code> -type construct would have the extra semantic information that all the iterations will be the same, except for potential side-effects.</p>
  
  <p>One option is to make a template </p>
  
  <pre><code>template&lt;class functor&gt;
  repeat(functor fun, unsigned n) {
     for (unsigned i = 0; i &lt; n; ++i)
       fun();
  }
  </code></pre>
  
  <p>and call</p>
  
  <pre><code>repeat([&amp;](){
    //do your thing
  }, repetitions)
  </code></pre>
  
  <p>but this really seems like overengineered overkill for a simple problem. This could be macroized to make the usage a bit nicer, but that certainly won't help with the overengineered feel.</p>
  
  <p>So one valid answer is that I'm on a wild goose chase here, and should just use the good old for loop with counter.</p>
  
  <p>Any standard C++ is fine, including upcoming standards.</p>
  
  <p>Related questions such as <a href="https://stackoverflow.com/questions/35401678/how-to-create-a-loop-in-c-that-loops-a-certain-amount-of-times">How to create a loop in C++ that loops a certain amount of times?</a>  and <a href="https://stackoverflow.com/questions/35401678/how-to-create-a-loop-in-c-that-loops-a-certain-amount-of-times">How to create a loop in C++ that loops a certain amount of times?</a> are beginners asking for some way to achieve this, whereas I'm specifically asking for a modern, clean and elegant way to achieve this. <a href="https://stackoverflow.com/questions/54924901/c-repeat-n-iterations">c++ repeat N iterations</a> is very close, though the difference here is that I'm asking for any alternatives, not necessarily included in <code>std::</code>.</p>