title: Using ranges::view::iota in parallel algorithms
body: <p>Since there is no index based <a href="https://en.cppreference.com/w/cpp/algorithm" rel="nofollow noreferrer">parallel for algorithm</a> in <a href="/questions/tagged/c%2b%2b17" class="post-tag" title="show questions tagged &#39;c++17&#39;" rel="tag">c++17</a>, I'm wondering if <code>ranges::view::iota</code> can be used in combination with <code>std::for_each</code> to emulate that. That is:</p>
  <pre><code>using namespace std;
  
  constexpr int N= 10'000'000;
  ranges::iota_view indices(0,N);
  vector&lt;int&gt; v(N);
  
  for_each(execution::par_unseq,indices.begin(),indices.end(),[&amp;](int i) { v[i]= i; });
  </code></pre>
  <p><code>iota_view</code> seems to provide random access for appropriate types (<a href="http://eel.is/c++draft/range.iota#iterator-1" rel="nofollow noreferrer">[range.iota.iterator]</a>):</p>
  <blockquote>
  <p><code>iota_view&lt;I, Bound&gt;::iterator::iterator_category</code> is defined as follows:</p>
  <p>(1.1) — If <code>I</code> models <code>Advanceable</code>, then <code>iterator_category</code> is <code>random_access_iterator_tag</code>.</p>
  <p>(1.2) — Otherwise, if <code>I</code> models <code>Decrementable</code>, then <code>iterator_category</code> is <code>bidirectional_iterator_tag</code>.</p>
  <p>(1.3) — Otherwise, if <code>I</code> models <code>Incrementable</code>, then <code>iterator_category</code> is <code>forward_iterator_tag</code>.</p>
  <p>(1.4) — Otherwise, <code>iterator_category</code> is <code>input_iterator_tag</code>.</p>
  </blockquote>
  <p>Is the above code correct? Is there any performance penalty in using <code>iota_view</code> this way?</p>
  <hr />
  <p><strong>EDIT:</strong> I've made some tests with <a href="https://github.com/ericniebler/range-v3" rel="nofollow noreferrer">range-v3</a>, <a href="https://github.com/CaseyCarter/cmcstl2" rel="nofollow noreferrer">cmcstl2</a>, and Intel's <a href="https://github.com/intel/parallelstl" rel="nofollow noreferrer">PSTL</a>.</p>
  <p>Using range-v3, the above example fails to compile with GCC 8. The compiler complains about <code>begin</code> and <code>end</code> having different types:</p>
  <pre><code>deduced conflicting types for parameter ‘_ForwardIterator’ (‘ranges::v3::basic_iterator&lt;ranges::v3::iota_view&lt;int, int&gt; &gt;’ and ‘ranges::v3::default_sentinel’)
  </code></pre>
  <p>Using cmcstl2 the code compiles cleanly, but it doesn't run in parallel. It seems to me that it falls back to the sequential version, maybe because the forward iterators requirements are somehow not met (<a href="https://godbolt.org/z/yvr-M2" rel="nofollow noreferrer">https://godbolt.org/z/yvr-M2</a>).</p>
  <p>There is a somewhat related PSTL issue (<a href="https://github.com/intel/parallelstl/issues/22" rel="nofollow noreferrer">https://github.com/intel/parallelstl/issues/22</a>).</p>